Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Import and Export MySQL databases using C++

3.68/5 (7 votes)
23 Mar 2015CPOL3 min read 30.2K   833  
This article explains how to import database to MySQL server and how to export database from MySQL server.

Introduction

This article shows you how to connect to a MySQL Server. It will also show you how you can import database to MySQL server and how you can export database from MySQL server using MySQL C API. 

Background

This is a C programming tutorial for the MySQL Server and import and export of database. It covers import and export of MySQL with the C API.

MySQL is a leading open source database management system. It is a multi user, multithreaded database management system. The development of MySQL begun in 1994 by a Swedish company MySQL AB. Sun Microsystems acquired MySQL AB in 2008. Sun was bought by Oracle in 2010. So today, Oracle corporation is the owner of the MySQL database. MySQL is developed in C/C++. Except of the C/C++, APIs exist for PHP, Python, Java, C#, Eiffel, Ruby, Tcl or Perl.

Prerequisites

  1. Install Visual Studio 2013 (As code along with article is developed in Visual Studio 2013)
  2. Install MySQL Server on your local machine 

Settings

Before going further details in writing database code using MySQL C APIs we need to configure our Visual Studio for MySQL using following settings:

  1. First of all copy "libmysql.dll" from location where MySQL is installed, on my PC it is situated at location, "C:\Program Files\MySQL\MySQL Server 5.6\lib". And then paste it at location where  your application's exe/dll will get created.
  2. Now it is time to configure Visual Studio to use MySQL C APIs: For that access project properties of your project and update the following settings accordingly:
    1. C/C++ -> General -> Additional Include Directories - C:\Program Files\MySQL\MySQL Server 5.6\include
    2. Linker -> General -> Additional Library Directories - C:\Program Files\MySQL\MySQL Server 5.6\include
    3. Linker -> Input -> Additional Dependencies - "C:\Program Files\MySQL\MySQL Server 5.6\lib\libmysql.lib"

Note: Please do needful changes according to your PC settings where MySQL server is installed.

Using the code

I have created a seperate class (CDatabaseManipulation) which deals with MySQL server connection, import database to MySQL server and export database from MySQL server. The class also contains some other functionalities to manipulate MySQL databases.

Connecting to MySQL server: To connect to the MySQL server user need to supply Server host name, user name and password. Database need to pass as blank. 

bool CDatabaseManipulation::Connect(/*[IN]*/const TCHAR *ptchHost,
                                    /*[IN]*/const TCHAR *ptchUserID,
                                    /*[IN]*/const TCHAR *ptchDatabase,
                                    /*[IN]*/const TCHAR *ptchPassword)
/* =====================================================================================================
NAME OF FUNCTION:    CDatabaseManipulation::Connect
CREDIT:              Satish Jagtap
PURPOSE:             This function is used to connect MYSQL database.
PARAMETERS:          [IN] 1) TCHAR *ptchHost: Parameter passed as ptchHost may be either a host name or
                             an IP address. If host is NULL or the string "localhost", a connection to
                             the local host is assumed.
                             For Windows, the client connects using a shared-memory connection,if the
                             server has shared-memory connections enabled. Otherwise, TCP/IP is used.
                     [IN] 2) TCHAR *ptchUserID: Parameter passed as ptchUserID contains the user's
                             MySQL login ID.
                     [IN] 3) TCHAR *ptchDatabase: Parameter passed as ptchDatabase is the database
                             name. If ptchDatabase is not NULL, the connection sets the default
                             database to this value.
                     [IN] 4) TCHAR *ptchPassword: Parameter passed as ptchPassword contains the 
                             password for user.
RETURN VALUE:        None
CALLS TO:            None
CALLED FROM:         None
Added date:          12 March, 2015
Updated date:         
======================================================================================================*/
{
    bool bConnectionSuccess = false;

    if(IsConnected()) 
    {
        m_objLogger.log(_T("Connection has already been established."));
        bConnectionSuccess = false;
    }

    //Allocates or initializes a MYSQL object suitable for mysql_real_connect()
    //returns an initialized MYSQL* handle. 
    //Returns NULL if there was insufficient memory to allocate a new object.
    if(mysql_init(&mysqlInitial) == NULL)
    {
        m_objLogger.log(_T("Failed to initiate MySQL connection"));
        bConnectionSuccess = false;
    }
    //Establishes a connection to a database server. 
    //Returns a MYSQL * handle or NULL if an error occurred.
    mysqlConnection = mysql_real_connect(&mysqlInitial, 
                                          (const char*)ptchHost, 
                                          (const char*)ptchUserID, 
                                          (const char*)ptchPassword, 
                                          (const char*)ptchDatabase, 0, 0, 0);
 
    // Check if connection succeeded.
    if( mysqlConnection == NULL )
    {
        LPTSTR lptstrError = new TCHAR[1024];

        _stprintf_s(lptstrError, 1024, _T("%s %s"), 
                    _T("Couldn't connect to MySQL database server! Error: "), 
                    mysql_error(mysqlConnection));
        m_objLogger.log(lptstrError);

        delete [] lptstrError;

        bConnectionSuccess = false;
    }
    else
    {
        m_objLogger.log(_T("Connect success."), _T("INFO")) ;
        bConnectionSuccess = true;
    }

    if(!IsConnected()) 
    {
        m_objLogger.log(GetError());
        bConnectionSuccess = false;
    }

    return bConnectionSuccess;
}

bool CDatabaseManipulation::CloseConnection(void) 
/* =====================================================================================================
NAME OF FUNCTION:    CDatabaseManipulation::CloseConnection
CREDIT:              Satish Jagtap
PURPOSE:             This function is used to close database connection if exist.
                     It should be non-NULL for MySQL.
PARAMETERS:          None
RETURN VALUE:        Returns connection status of database.
CALLS TO:            1) IsConnected()
CALLED FROM:         2) Destructor i.e. from ~CDatabaseManipulation()
Added date:          12 March, 2015
Updated date:         
======================================================================================================*/
{
    bool bSuccessCloseConnection = false;

    if(IsConnected()) 
    {
        mysql_close(mysqlConnection);
        mysqlConnection = (MYSQL *)NULL;

        bSuccessCloseConnection = true;
    }
    else
    {
        bSuccessCloseConnection = false;
    }

    return bSuccessCloseConnection;
}

Import database to MySQL server: To import database user need to connect to MySQL server. Along with other details user need to pass database name and import file (with .sql extension) location to ImportDatabase function.

Note: For function called from ImportDatabase function please refer  to the class in the application code along with this article

bool CDatabaseManipulation::ImportDatabase(/*[IN]*/const TCHAR *ptchHost, 
                                           /*[IN]*/const TCHAR *ptchUserID, 
                                           /*[IN]*/const TCHAR *ptchPassword, 
                                           /*[IN]*/const TCHAR *ptchDatabaseNameToImport, 
                                           /*[IN]*/const TCHAR *ptchImportDatabaseFile)
/* =====================================================================================================
NAME OF FUNCTION:   CDatabaseManipulation::ImportDatabase
CREDIT:             Satish Jagtap
PURPOSE:            This function is used to import database using import file into MySql database.
                    This function create vector of strings containing commands to import database.
                    This function then creates batch file.
                    This function then writes vector of commands into batch file.
                    This function then execute batch file using cmd.exe.
                    At the end after import of database, function removes batch file.
PARAMETERS:         [IN] 1) TCHAR *ptchHost: Host or server name to connect and import database.
                    [IN] 2) TCHAR *ptchUserID: User name to connect and import database.
                    [IN] 3) TCHAR *ptchPassword: Password to connect and import database.
                    [IN] 4) TCHAR *ptchDatabaseNameToImport: MySql database name to import.
                    [IN] 5) TCHAR *ptchImportDatabaseFile: Database file to import into MySql database.
RETURN VALUE:       None
CALLS TO:           1) WriteVectorInFile
                    2) GetExecutablePath
CALLED FROM:        None
Added date:         17 March, 2015
Updated date:
======================================================================================================*/
{
    bool bImportDBSuccess = false;

    //Database connection
    //Connect(ptchHost, ptchUserID, _T(""), ptchPassword);
    if(!IsConnected()) 
    {
        m_objLogger.log(_T("MySql server is not connected."));
        bImportDBSuccess = false;
    }
    else
    {
        TCHAR *strCreateDatabaseCommand = new TCHAR[MAX_PATH];
        _tcscpy_s(strCreateDatabaseCommand, MAX_PATH, _T("CREATE DATABASE "));
        _tcscat_s(strCreateDatabaseCommand, MAX_PATH, ptchDatabaseNameToImport);
        mysql_query(mysqlConnection, (const char*)strCreateDatabaseCommand);

        //Creating batch file data to execute
        TCHAR strProgramFilePath[MAX_PATH];
        SHGetSpecialFolderPath(0, strProgramFilePath, CSIDL_PROGRAM_FILES, FALSE);

        TCHAR * strReturnSQLFilePath = new TCHAR[MAX_PATH];
        _tcscpy_s(strReturnSQLFilePath, MAX_PATH, _T(""));

        SearchForFilePath(strProgramFilePath, _T("mysql.exe"), strReturnSQLFilePath);

        if(!_tcscmp(strReturnSQLFilePath, _T("")))
        {
            return false;
        }

        //populate vector with import database command to write into batch file for import database
        vector<TCHAR *> vecToWriteInFile;
        vecToWriteInFile.push_back(_T("@echooff"));
        vecToWriteInFile.push_back(_T("set MainDir = %CD%"));
        vecToWriteInFile.push_back(_T("CD %MainDir%"));

        TCHAR strSQLDrive[3];
        strSQLDrive[0] = strReturnSQLFilePath[0];
        strSQLDrive[1] = strReturnSQLFilePath[1];
        strSQLDrive[2] = _T('\0');
        vecToWriteInFile.push_back(strSQLDrive);

        TCHAR * strTempPath = new TCHAR[MAX_PATH];
        _tcscpy_s(strTempPath, MAX_PATH, _T("CD "));
        _tcscat_s(strTempPath, MAX_PATH, strReturnSQLFilePath);
        vecToWriteInFile.push_back(strTempPath);

        TCHAR strImportCommand[1024];
        _tcscpy_s(strImportCommand, MAX_PATH, _T("mysql --user="));
        _tcscat_s(strImportCommand, ptchUserID);
        _tcscat_s(strImportCommand, _T(" --password="));
        _tcscat_s(strImportCommand, ptchPassword);
        _tcscat_s(strImportCommand, _T(" -D"));
        _tcscat_s(strImportCommand, ptchDatabaseNameToImport);
        _tcscat_s(strImportCommand, _T(" < \""));
        _tcscat_s(strImportCommand, ptchImportDatabaseFile);
        _tcscat_s(strImportCommand, _T("\""));
        vecToWriteInFile.push_back(strImportCommand);
        vecToWriteInFile.push_back(_T("exit"));

        //Create temporary import batch file
        CExecutablePathInfo objExecutablePathInfo;
        LPTSTR lptstrExecutableDirectory = new TCHAR[1024];
        objExecutablePathInfo.GetExecutableDirectory(lptstrExecutableDirectory, 1024);
        _tcscat_s(lptstrExecutableDirectory, MAX_PATH, _T("\\TempDatabaseManipulationImport.bat"));

        //Write into temporary created import batch file
        WriteVectorInFile(vecToWriteInFile, lptstrExecutableDirectory);

        vecToWriteInFile.clear(); //clears the vector
        vecToWriteInFile.shrink_to_fit(); //It requests the removal of unused capacity of vector

        TCHAR strSystemDirPath[MAX_PATH] = _T("");
        GetSystemDirectory(strSystemDirPath, sizeof(strSystemDirPath) / sizeof(_TCHAR));

        // path to cmd.exe, path to batch file, plus some space for quotes, spaces, etc.
        TCHAR strCommandLine[2 * MAX_PATH + 16] = _T("");

        _sntprintf_s(strCommandLine, sizeof(strCommandLine) / sizeof(_TCHAR), 
                     _T("\"%s\\cmd.exe\" /C \"%s\""), strSystemDirPath, lptstrExecutableDirectory);

        delete[] strTempPath;
        strTempPath = NULL;
        delete[] strReturnSQLFilePath;
        strReturnSQLFilePath = NULL;

        STARTUPINFO si = { 0 }; // alternative way to zero array
        si.cb = sizeof(si);
        PROCESS_INFORMATION pi = { 0 };

        if (!CreateProcess(NULL,
                            strCommandLine,
                            NULL,
                            NULL,
                            FALSE,
                            0,
                            NULL,
                            NULL,
                            &si,
                            &pi)
            )
        {
            LPTSTR lpstrError = new TCHAR[1024];

            _stprintf_s(lpstrError, 1024, _T("CreateProcess failed (%d)\n"), GetLastError());
                        m_objLogger.log(lpstrError);

            delete[] lpstrError;

            bImportDBSuccess = false;
        }
        else
        {
            bImportDBSuccess = true;
        }

        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);

        //Remove batch file
        remove((const char*)lptstrExecutableDirectory);

        delete[] lptstrExecutableDirectory;
        lptstrExecutableDirectory = NULL;
    }

    return bImportDBSuccess;
}

Retrieve databases list from currently running MySqlServer: To export database from MySQL server we need to know databases currently available in currently running MySQL server. This section retrieves database list from MySQL server.

void CDatabaseManipulation::RetrieveDatabasesListInMySqlServer(vector<TCHAR*> &vecMySqlDatabasesList)
/* =====================================================================================================
NAME OF FUNCTION:   CDatabaseManipulation::RetrieveDatabasesListInMySqlServer
CREDIT:             Satish Jagtap
PURPOSE:            This function is used to receives databases list in currently running MySql server 
                    instance
PARAMETERS:         1) vector<TCHAR*> &vecMySqlDatabasesList - Receives databases list in currently 
                                                               running MySql server instance
RETURN VALUE:       None
CALLS TO:           None
CALLED FROM:        None
Added date:         12 March, 2015
Updated date:
======================================================================================================*/
{
    MYSQL_RES *myqlResult = mysql_list_dbs(mysqlConnection, _T("%") /* fetch all */);

    if (!myqlResult) 
    {
        LPTSTR lptstrError = new TCHAR[1024];

        _stprintf_s(lptstrError, 1024, _T("Couldn't get db list: %s"), GetError());
        m_objLogger.log(lptstrError);

        delete [] lptstrError;
    }
    else 
    {
        MYSQL_ROW mysqlRow;

        while(mysqlRow = mysql_fetch_row(myqlResult)) 
        {
            if((_tcscmp(mysqlRow[0], "information_schema")) && 
               (_tcscmp(mysqlRow[0], "performance_schema")) && 
               (_tcscmp(mysqlRow[0], "mysql")))
            {
                vecMySqlDatabasesList.push_back(mysqlRow[0]);
            }
        }
    }
}

Export database from MySQL server: To export database user need to connect to MySQL server. Along with other details user need to pass database name and export file (with .sql extension) location to ExportDatabase function.

Note: For function called from ExportDatabase function please refer  to the class in the application code along with this article

C++
bool CDatabaseManipulation::ExportDatabase(/*[IN]*/const TCHAR *ptchHost,
                                           /*[IN]*/const TCHAR *ptchUserID, 
                                           /*[IN]*/const TCHAR *ptchPassword, 
                                           /*[IN]*/const TCHAR *ptchDatabaseNameToExport, 
                                           /*[IN]*/const TCHAR *ptchExportDatabaseFileWithPath)
/* ======================================================================================================
NAME OF FUNCTION:   CDatabaseManipulation::ExportDatabase
CREDIT:             Satish Jagtap
PURPOSE:            This function is used to export database to the specified path with specified file 
                    name.
PARAMETERS:         [IN] 1) TCHAR *ptchHost: Host or server name to connect and import database.
                    [IN] 2) TCHAR *ptchUserID: User name to connect and import database.
                    [IN] 3) TCHAR *ptchPassword: Password to connect and import database.
                    [IN] 4) TCHAR *ptchDatabaseNameToExport: MySql database name to export.
                    [IN] 5) TCHAR *ptchExportDatabaseFileWithPath: Database filename with path to 
                            export MySql database.
RETURN VALUE:       Returns true on success.
CALLS TO:           1) WriteVectorInFile
                    2) GetExecutablePath
CALLED FROM:        None
Added date:         17 March, 2015
Updated date:
=======================================================================================================*/
{
    bool bExportDBSuccess = false;

    //Database connection
    //Connect(ptchHost, ptchUserID, ptchDatabaseNameToExport, ptchPassword);
    if(!IsConnected()) 
    {
        m_objLogger.log(_T("MySql server is not connected."));
        bExportDBSuccess = false;
    }
    else
    {
        //Creating batch file data to execute
        TCHAR strProgramFilePath[MAX_PATH];
        SHGetSpecialFolderPath(0, strProgramFilePath, CSIDL_PROGRAM_FILES, FALSE);

        TCHAR * strReturnSQLFilePath = new TCHAR[MAX_PATH];
        _tcscpy_s(strReturnSQLFilePath, MAX_PATH, _T(""));

        SearchForFilePath(strProgramFilePath, _T("mysqldump.exe"), strReturnSQLFilePath);

        if(!_tcscmp(strReturnSQLFilePath, _T("")))
        {
            return false;
        }

        //populate vector with export database command to write into batch file for export database
        vector<TCHAR *> vecToWriteInFile;
        vecToWriteInFile.push_back(_T("@echooff"));
        vecToWriteInFile.push_back(_T("set MainDir = %CD%"));
        vecToWriteInFile.push_back(_T("CD %MainDir%"));

        TCHAR strSQLDrive[3];
        strSQLDrive[0] = strReturnSQLFilePath[0];
        strSQLDrive[1] = strReturnSQLFilePath[1];
        strSQLDrive[2] = _T('\0');
        vecToWriteInFile.push_back(strSQLDrive);

        TCHAR * strTempPath = new TCHAR[MAX_PATH];
        _tcscpy_s(strTempPath, MAX_PATH, _T("CD "));
        _tcscat_s(strTempPath, MAX_PATH, strReturnSQLFilePath);
        vecToWriteInFile.push_back(strTempPath);

        TCHAR strImportCommand[1024];
        _tcscpy_s(strImportCommand, MAX_PATH, _T("mysqldump --user="));
        _tcscat_s(strImportCommand, ptchUserID);
        _tcscat_s(strImportCommand, _T(" --password="));
        _tcscat_s(strImportCommand, ptchPassword);
        _tcscat_s(strImportCommand, _T(" --result-file="));
        _tcscat_s(strImportCommand, _T("\""));
        _tcscat_s(strImportCommand, ptchExportDatabaseFileWithPath);
        _tcscat_s(strImportCommand, _T("\""));
        _tcscat_s(strImportCommand, _T(" "));
        _tcscat_s(strImportCommand, ptchDatabaseNameToExport);
        vecToWriteInFile.push_back(strImportCommand);
        vecToWriteInFile.push_back(_T("exit"));

        //Create temporary import batch file
        CExecutablePathInfo objExecutablePathInfo;
        LPTSTR lptstrExecutableDirectory = new TCHAR[1024];
        objExecutablePathInfo.GetExecutableDirectory(lptstrExecutableDirectory, 1024);
        _tcscat_s(lptstrExecutableDirectory, MAX_PATH, _T("\\TempDatabaseManipulationExport.bat"));

        //Write into temporary created import batch file
        WriteVectorInFile(vecToWriteInFile, lptstrExecutableDirectory);
    
        vecToWriteInFile.clear(); //clears the vector
        vecToWriteInFile.shrink_to_fit(); //It requests the removal of unused capacity of vector

        TCHAR strSystemDirPath[MAX_PATH] = _T("");
        GetSystemDirectory(strSystemDirPath, sizeof(strSystemDirPath) / sizeof(_TCHAR));

        // path to cmd.exe, path to batch file, plus some space for quotes, spaces, etc.
        TCHAR strCommandLine[2 * MAX_PATH + 16] = _T("");

        _sntprintf_s(strCommandLine, sizeof(strCommandLine) / sizeof(_TCHAR), 
                     _T("\"%s\\cmd.exe\" /C \"%s\""), strSystemDirPath, lptstrExecutableDirectory);

        delete [] strTempPath;
        strTempPath = NULL;
        delete [] strReturnSQLFilePath;
        strReturnSQLFilePath = NULL;

        STARTUPINFO si = { 0 }; // alternative way to zero array
        si.cb = sizeof(si);
        PROCESS_INFORMATION pi = { 0 };

        if (!CreateProcess(NULL,
                            strCommandLine,
                            NULL,
                            NULL,
                            FALSE,
                            0,
                            NULL,
                            NULL,
                            &si,
                            &pi)
            )
        {
            LPTSTR lpstrError = new TCHAR[1024];

            _stprintf_s(lpstrError, 1024, _T("CreateProcess failed (%d)\n"), GetLastError());
            m_objLogger.log(lpstrError);

            delete [] lpstrError;

            bExportDBSuccess = false;
        }
        else
        {
            bExportDBSuccess = true;
        }

        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);

        //Remove batch file
        remove((const char*)lptstrExecutableDirectory);

        delete [] lptstrExecutableDirectory;
        lptstrExecutableDirectory = NULL;
    }

    return bExportDBSuccess;
}

Application code along with this article is whole source code developed using MFC dialog based application. Here is the application window will look like:

Image 1Image 2

Steps to use application:

  1. First connect to MySQL server using "Connect to MySql Server Instance" section.
  2. Import database using "Import Database" section.
  3. Export database using "Export Database" section.

Points of Interest

To import and export database, I have created respective(for import/export database) batch files and run those batch files on respective events. I have deleted those batch files after completion of event (import/export).

Remarks

  1. Remember to change your Project Settings to point to the MySQL include files and the MySQL libs.
  2. Copy "libmysql.dll" from location where MySQL is installed, on my PC it is situated at location, "C:\Program Files\MySQL\MySQL Server 5.6\lib". And then paste it at location where  your application's exe/dll will get created.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)