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
- Install Visual Studio 2013 (As code along with article is developed in Visual Studio 2013)
- 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:
- 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.
- 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:
- C/C++ -> General -> Additional Include Directories - C:\Program Files\MySQL\MySQL Server 5.6\include
- Linker -> General -> Additional Library Directories - C:\Program Files\MySQL\MySQL Server 5.6\include
- 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:
- Server Host name
- User name
- Password
- Database name to import
- 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(const TCHAR *ptchHost,
const TCHAR *ptchUserID,
const TCHAR *ptchPassword,
const TCHAR *ptchDatabaseNameToImport,
const TCHAR *ptchImportDatabaseFileWithPath)
{
bool bImportDBSuccess = false;
TCHAR strProgramFilePath[MAX_PATH] = { 0 };
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"));
_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("\""));
#pragma region "Achieve import database process using CreateProcess API"
#pragma endregion
#pragma region "Achieve import database process using ShellExecute API"
UINT nReturnErrorCode = (UINT)ShellExecute(NULL,
_T("open"),
strReturnSQLFilePath,
strImportCommand,
NULL,
SW_HIDE);
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)
{
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 );
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)
{
MYSQL_RES *myqlResult = mysql_list_dbs(mysqlConnection, _T("%") );
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:
- User name
- Password
- Database name to export
- 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(const TCHAR *ptchUserID,
const TCHAR *ptchPassword,
const TCHAR *ptchDatabaseNameToExport,
const TCHAR *ptchExportDatabaseFileWithPath)
{
bool bExportDBSuccess = false;
TCHAR strProgramFilePath[MAX_PATH] = { 0 };
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"));
_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);
#pragma region "Achieve export database process using CreateProcess API"
#pragma endregion
#pragma region "Achieve export database process using ShellExecute API"
UINT nReturnErrorCode = (UINT)ShellExecute(NULL,
_T("open"),
strReturnSQLFilePath,
strExportCommand,
_T(""),
SW_HIDE);
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:
- First enter required details in " Enter MySQL server details" section.
- Import database using "Import Database" section.
- 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
- Remember to change your Project Settings to point to the MySQL include files and the MySQL libs.
- 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.