Click here to Skip to main content
15,880,725 members
Articles / Desktop Programming / MFC

Another way to Import and Export MySQL database using C++

Rate me:
Please Sign up or sign in to vote.
4.44/5 (4 votes)
31 Mar 2015CPOL5 min read 13.4K   292   10  
This article explains how to import database to MySQL server and how to export database from MySQL server without using batch files.

Introduction

First of all, I want to thank Mr. Imagiro who gave valuable comments to my last article. Their comments forced me to work on import and export database from/to MySQL server without using batch files. In this article I tried to develop functionality for import and export database which supports both "Unicode" as well as "MBCS" character set. This article shows you how to import or export database to/from MySQL server without connection to MySQL server even without using MySQL C API. 

Background

This is a C programming tutorial for the MySQL Server to import and export of database. It covers import and export of MySQL without using the C API provided by MySQL.

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 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 only with  import database to MySQL server and export database from MySQL server. The class also contains connection to MySQL which is only used in retrieving database list from MySQL server which ease our job while exporting database from MySQL server. For details you need to go through application code along with this article. So it is upto the user of this class whether he is interested in retrieving database list from MySQL server or not. 

To use this class in your application, you first need to update your Visual Studio settings as explained in "Settings" section. Then you need to include, "DatabaseManipulation.h" file where you want to use class's functionality. Then create object of the class and access functionality provided by "CDatabaseManipulation" class.

Import database to MySQL server: To import database user need to provide following information:

  1. Server Host name
  2. User name
  3. Password
  4. Database name to import
  5. Database import file (with .sql extension) with its location

Here we can achieve our task of importing database using following 2 ways:
        1) Using CreateProcess API
        2) Using ShellExecute API

It is upto user to use functionality according to their need.

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 *ptchImportDatabaseFileWithPath)
/* ===================================================================================================
NAME OF FUNCTION:   CDatabaseManipulation::ImportDatabase
CREDIT:             Satish Jagtap
PURPOSE:            This function is used to import MySQL database.
Special Note:       Here we can achieve our task of importing database using following 2 ways:
                    1) Using CreateProcess API
                    2) Using ShellExecute API
                    User of this class can modify this function according to his/her need.
                    Means user can remove other section            
PARAMETERS:         [IN]  1) const TCHAR *ptchHost: Server host name
                    [IN]  2) const TCHAR *ptchUserID: User of MySQL server.
                    [IN]  3) const TCHAR *ptchPassword: Password  of MySQL server.
                    [IN]  4) const TCHAR *ptchDatabaseNameToImport: Database to import.
                    [IN]  5) constTCHAR *ptchImportDatabaseFileWithPath: Database file to import.
RETURN VALUE:       Returns true on success.
CALLS TO:           1) SearchForFilePath
CALLED FROM:        None
Added date:         31 March, 2015
Updated date:
=====================================================================================================*/
{
    bool bImportDBSuccess                = false;
    TCHAR strProgramFilePath[MAX_PATH]    = { 0 };

    //Retreive program file path
    if(!SHGetSpecialFolderPath(0, strProgramFilePath, CSIDL_PROGRAM_FILES, FALSE)) 
    {
        LPTSTR lpstrError = new TCHAR[1024];

        _stprintf_s(lpstrError, 1024, _T("Unable to retrieve program file path (%d)\n"), 
                                      GetLastError());
        m_objLogger.log(lpstrError);

        delete[] lpstrError;

        bImportDBSuccess = false;
    }
    else
    {
        TCHAR *strReturnSQLFilePath = new TCHAR[MAX_PATH];
        TCHAR *strImportCommand = new TCHAR[MAX_PATH];

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

        _tcscat_s(strReturnSQLFilePath, MAX_PATH, _T("\\mysql.exe"));

        //Populate command to import database
        _tcscpy_s(strImportCommand, MAX_PATH, _T("-u"));
        _tcscat_s(strImportCommand, MAX_PATH, ptchUserID);
        _tcscat_s(strImportCommand, MAX_PATH, _T(" -p"));
        _tcscat_s(strImportCommand, MAX_PATH, ptchPassword);
        _tcscat_s(strImportCommand, MAX_PATH, _T(" -h "));
        _tcscat_s(strImportCommand, MAX_PATH, ptchHost);
        _tcscat_s(strImportCommand, MAX_PATH, _T(" -e "));
        _tcscat_s(strImportCommand, MAX_PATH, _T(" \"drop database if exists "));
        _tcscat_s(strImportCommand, MAX_PATH, ptchDatabaseNameToImport);
        _tcscat_s(strImportCommand, MAX_PATH, _T("; "));
        _tcscat_s(strImportCommand, MAX_PATH, _T(" create database "));
        _tcscat_s(strImportCommand, MAX_PATH, ptchDatabaseNameToImport);
        _tcscat_s(strImportCommand, MAX_PATH, _T("; "));
        _tcscat_s(strImportCommand, MAX_PATH, _T("use "));
        _tcscat_s(strImportCommand, MAX_PATH, ptchDatabaseNameToImport);
        _tcscat_s(strImportCommand, MAX_PATH, _T(";\\. "));
        _tcscat_s(strImportCommand, MAX_PATH, ptchImportDatabaseFileWithPath);
        _tcscat_s(strImportCommand, MAX_PATH, _T("\""));

        /***************************************************************************************
        Here we can achieve our task of importing database using following 2 ways:
        1) Using CreateProcess API
        2) Using ShellExecute API
        Note: I have just commented "Achieve import database process using CreateProcess API"
              section
        ***************************************************************************************/
    #pragma region "Achieve import database process using CreateProcess API"
        //LPTSTR strImportDatabaseCommandLine = new TCHAR[1024];

        //_stprintf_s(strImportDatabaseCommandLine, 1024, _T("%s %s"), strReturnSQLFilePath, 
        //                                                             strImportCommand);

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

        //if (!CreateProcess(NULL,
        //                    strImportDatabaseCommandLine,
        //                    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);

        //delete [] strImportDatabaseCommandLine;
        //strImportDatabaseCommandLine = NULL;
    #pragma endregion

    #pragma region "Achieve import database process using ShellExecute API"
        UINT nReturnErrorCode = (UINT)ShellExecute(NULL, 
                                                    _T("open"),
                                                    strReturnSQLFilePath, //mysql.exe file path
                                                    strImportCommand, //command to import database 
                                                                      //into MySQL server
                                                    NULL, 
                                                    SW_HIDE); //hides command window

        if(nReturnErrorCode <= 32)
        {
            LPTSTR lpstrError = new TCHAR[1024];

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

            delete[] lpstrError;

            bImportDBSuccess = false;
        }
        else
        {
            bImportDBSuccess = true;
        }
    #pragma endregion

        delete [] strReturnSQLFilePath;
        strReturnSQLFilePath = NULL;
        delete [] strImportCommand;
        strImportCommand = 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. Here I have created two versions of this function one used with Unicode character set and another with MBCS.

#pragma region Retrieve databases list from currently running MySqlServer

#ifdef UNICODE

void CDatabaseManipulation::RetrieveDatabasesListInMySqlServer(vector<wchar_t*> &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<wchar_t*> &vecMySqlDatabasesList - Receives databases list in currently 
                                                                 running MySql server instance
RETURN VALUE:       None
CALLS TO:           None
CALLED FROM:        None
Added date:         27 March, 2015
Updated date:
====================================================================================================*/
{
    char strTemp[MAX_PATH]        = { 0 };
    size_t nTempLen                = 0;
    
    wcstombs_s(&nTempLen, strTemp, _T("%"), wcslen(_T("%")) + 1);
    MYSQL_RES *myqlResult = mysql_list_dbs(mysqlConnection, strTemp /* 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)) 
        {
            size_t nLen                    = 0;
            int nLenInfoSchema            = strlen(mysqlRow[0]) + 1;
            wchar_t wcstrRow[MAX_PATH]    = { 0 };

            mbstowcs_s(&nLen, wcstrRow, mysqlRow[0], nLenInfoSchema);

            if((_tcscmp(wcstrRow, _T("information_schema"))) && 
               (_tcscmp(wcstrRow, _T("performance_schema"))) && 
               (_tcscmp(wcstrRow, _T("mysql"))))
            {
                size_t nTempLen                = 0;
                wchar_t strRow[MAX_PATH]    = { 0 };

                strMySQLResultRow = new wchar_t[MAX_PATH];

                mbstowcs_s(&nTempLen, strRow, mysqlRow[0], strlen(mysqlRow[0]) + 1);
                _tcscpy_s(strMySQLResultRow, _tcslen(strRow) + 1, strRow);

                vecMySqlDatabasesList.push_back(strMySQLResultRow);
            }
        }
    }
}
#else
void CDatabaseManipulation::RetrieveDatabasesListInMySqlServer(vector<CHAR*> &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<CHAR*> &vecMySqlDatabasesList - Receives databases list in currently 
                                                              running MySql server instance
RETURN VALUE:       None
CALLS TO:           None
CALLED FROM:        None
Added date:         21 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], _T("information_schema"))) && (_tcscmp(mysqlRow[0], _T("performance_schema"))) && (_tcscmp(mysqlRow[0], _T("mysql"))))
            {
                vecMySqlDatabasesList.push_back(mysqlRow[0]);
            }
        }
    }
}
#endif
#pragma endregion

Export database to MySQL server: To export database user need to provide following information:

  1. User name
  2. Password
  3. Database name to export
  4. Database export file (with .sql extension) with its location

Here we can achieve our task of exporting database using following 2 ways:
        1) Using CreateProcess API
        2) Using ShellExecute API

It is upto user to use functionality according to their need.

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

bool CDatabaseManipulation::ExportDatabase(/*[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 MySQL database.
Special Note:       Here we can achieve our task of exporting database using following 2 ways:
                    1) Using CreateProcess API
                    2) Using ShellExecute API
                    User of this class can modify this function according to his/her need.
                    Means user can remove other section    
PARAMETERS:         [IN]  1) const TCHAR *ptchUserID: User of MySQL server.
                    [IN]  2) const TCHAR *ptchPassword: Password  of MySQL server.
                    [IN]  3) const TCHAR *ptchDatabaseNameToExport: Database to export.
                    [IN]  4) constTCHAR *ptchExportDatabaseFileWithPath: Database file to export.
RETURN VALUE:       Returns true on success.
CALLS TO:           1) SearchForFilePath
CALLED FROM:        None
Added date:         31 March, 2015
Updated date:
====================================================================================================*/
{
    bool bExportDBSuccess                = false;
    TCHAR strProgramFilePath[MAX_PATH]    = { 0 };

    //Retreive program file path
    if(!SHGetSpecialFolderPath(0, strProgramFilePath, CSIDL_PROGRAM_FILES, FALSE)) 
    {
        LPTSTR lpstrError = new TCHAR[1024];

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

        delete[] lpstrError;

        bExportDBSuccess = false;
    }
    else
    {
        TCHAR *strReturnSQLFilePath = new TCHAR[MAX_PATH];
        TCHAR *strExportCommand        = new TCHAR[MAX_PATH];

        _tcscpy_s(strReturnSQLFilePath, MAX_PATH, _T(""));
        SearchForFilePath(strProgramFilePath, _T("mysqldump.exe"), strReturnSQLFilePath);
        _tcscat_s(strReturnSQLFilePath, MAX_PATH, _T("\\mysqldump.exe"));

        //Populate command to export database
        _tcscpy_s(strExportCommand, MAX_PATH, _T(" --user="));
        _tcscat_s(strExportCommand, MAX_PATH, ptchUserID);
        _tcscat_s(strExportCommand, MAX_PATH, _T(" --password="));
        _tcscat_s(strExportCommand, MAX_PATH, ptchPassword);
        _tcscat_s(strExportCommand, MAX_PATH, _T(" "));
        _tcscat_s(strExportCommand, MAX_PATH, ptchDatabaseNameToExport);
        _tcscat_s(strExportCommand, MAX_PATH, _T(" -r "));
        _tcscat_s(strExportCommand, MAX_PATH, ptchExportDatabaseFileWithPath);

        /***************************************************************************************
        Here we can achieve our task of exporting database using following 2 ways:
        1) Using CreateProcess API
        2) Using ShellExecute API
        Note: I have just commented "Achieve export database process using CreateProcess API"
              section
        ***************************************************************************************/
    #pragma region "Achieve export database process using CreateProcess API"
        //LPTSTR strExportDatabaseCommandLine = new TCHAR[1024];

        //_stprintf_s(strExportDatabaseCommandLine, 1024, _T("%s %s"), strReturnSQLFilePath, 
        //                                                             strExportCommand);

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

        //if (!CreateProcess(NULL,
        //                    strExportDatabaseCommandLine,
        //                    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);

        //delete [] strExportDatabaseCommandLine;
        //strExportDatabaseCommandLine = NULL;
    #pragma endregion

    #pragma region "Achieve export database process using ShellExecute API"
        UINT nReturnErrorCode = (UINT)ShellExecute(NULL, 
                                                    _T("open"), 
                                                    strReturnSQLFilePath, //mysqldump.exe file path
                                                    strExportCommand, //command to import 
                                                                      //database into MySQL server
                                                    _T(""), 
                                                    SW_HIDE); //hides command window

        if(nReturnErrorCode <= 32)
        {
            LPTSTR lpstrError = new TCHAR[1024];

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

            delete[] lpstrError;

            bExportDBSuccess = false;
        }
        else
        {
            bExportDBSuccess = true;
        }
    #pragma endregion

        delete [] strReturnSQLFilePath;
        strReturnSQLFilePath = NULL;
        delete [] strExportCommand;
        strExportCommand = NULL;
    }

    return bExportDBSuccess;
}

Application code along with this article is whole source code developed using MFC dialog based application. Application code in this article is much more different from my last article. Here is the application window will look like:

Steps to use application:

  1. First enter required details in " Enter MySQL server details" section.
  2. Import database using "Import Database" section.
  3. To export database first retrieve database list using "Retrieve Database List" button. Application comes with editable combo box ("Select Database") where user manually enter database name which he/she wants to export by skipping "Retrieve Database List" functionality. Then user can able to export database by providing further details in "Export Database" section.

Points of Interest

As in my last article "Import and Export MySQL databases using C++" I have created batch files to import and export database. But in this article I have skipped that part and provided different functionality to import and export database to/from MySQL server. Also I have created this source code to work with Unicode character set as well as multibyte character set.

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)



Comments and Discussions

 
-- There are no messages in this forum --