Mysql imports the C ++ implementation and encapsulation of the source database SQL, mysqlsource
Many people have had headaches for this issue before:
I want to have a database script that can be imported without installing the mysql client, but cannot find the corresponding api call. Therefore, you must import the database as follows:
Common. h
# Ifndef _ COMMON_H # define _ COMMON_H # ifdef WIN32 # include <winsock2.h> typedef _ struct; typedef _ int16int16_t; typedef _ int32int32_t; typedef _ int64int64_t; typedef unsigned _ struct; typedef unsigned _ int16uint16_t; typedef unsigned _ int32uint32_t; typedef unsigned _ struct; # define atoll (_ String) \ _ atoi64 (_ String) # else # include <sys/types. h> # include <sys/socket. h> # include <stdint. h> # include <linux/string. h> # endif # include <stdlib. h> # include <stdio. h> # include <string> # include <vector> # include <map> # include <set> # include <list> using namespace std; enum SERVER_ACTION {SERVER_STARTNEW = 0, // open new server}; # ifdef WIN32 # define PATH_DELIMTER "\" # else # define PATH_DELIMTER "/" # endif
Dbmanager. h
# Ifndef _ DBMANAGER_H # define _ DBMANAGER_H # include <string> using namespace std; # include "common. h "# include <mysql. h> // database configuration information struct DBInfo {string host; string user; string passwd; string db; uint16_t port ;}; class DBManager {public: DBManager ();~ DBManager (); bool SelectDB (string dbName); bool ConnectDB (DBInfo & dbInfo); MYSQL_RES * ExeSql (const char * SQL, int len); bool readFromSql (string fileName, vector <string> & SQL); bool sourceSql (string fileName); private: MYSQL * mysqlInit (DBInfo & info); void mysqlClose (); private: MYSQL * m_mysqlConn; DBInfo m_dbConfig;}; extern DBManager g_DBManager; # endif
Dbmanager. cpp
# Include "dbmanager. h" # include <fstream> DBManager g_DBManager; DBManager: DBManager () {} DBManager ::~ DBManager () {} bool DBManager: ConnectDB (DBInfo & dbInfo) {m_dbConfig = dbInfo; m_mysqlConn = mysqlInit (dbInfo); if (! M_mysqlConn) {return false;} return true;} MYSQL * DBManager: mysqlInit (DBInfo & info) {MYSQL * mysql = mysql_init (NULL); if (! Mysql) return NULL; if (! Mysql_real_connect (mysql, info. host. c_str (), info. user. c_str (), info. passwd. c_str (), info. db. c_str (), info. port, NULL, 0) {int ret = mysql_errno (mysql); mysql_close (mysql); return NULL ;}# if MYSQL_VERSION_ID> = 50013my_bool reconnect = 1; if (mysql_options (mysql, MYSQL_OPT_RECONNECT, & reconnect) {int ret = mysql_errno (mysql); mysql_close (mysql); return NULL ;}# elsemysql-> reconnect = 1; # endifreturn mysql;} v Oid DBManager: mysqlClose () {if (m_mysqlConn) {mysql_close (m_mysqlConn); m_mysqlConn = NULL ;}} /*************************************** * ******************************** // Execute SQL statements * //************************************** * ********************************/MYSQL_RES * DBManager:: ExeSql (const char * SQL, int len) {MYSQL_RES * res = NULL; int ret = mysql_real_query (m_mysqlConn, SQL, len); if (ret = 0) {res = Mysql_store_result (m_mysqlConn);} else {printf ("mysql query % s return errorcode: % d \ n", SQL, mysql_errno (m_mysqlConn);} return res ;} /*************************************** * ********************************* select a database */ /*************************************** */bool DBManager:: SelectDB (string dbName) {if (mysql_select_db (m_mysqlConn, dbName. c_str () return false; else ret Urn true ;} /*************************************** * ******************************** // * fileName is an SQL File path, parse each SQL statement in fileName, put it in the SQL container *//******************************** **************************************** /bool DBManager:: readFromSql (string fileName, vector <string> & SQL) {ifstream in (fileName. c_str (), ios: in); // linuxstring signalSql, s; if (! In) {return false;} while (getline (in, s) {int pos = s. find (";"); signalSql + = s; if (pos! = S. npos) {// locate the delimiter SQL of a statement. push_back (signalSql); signalSql. clear ();} s. clear ();} in. close (); return true ;} /*************************************** * ********************************* // * import database SQL * //************************************** */bool DBManager:: sourceSql (string fileName) {vector <string> vecSql; bool ret = readFromSql (fileName, vecSql); if (ret = false) {printf ("Import gamed B. SQL failed "); return false;} for (vector <string >:: iterator it = vecSql. begin (); it! = VecSql. end (); it ++) {ExeSql (* it). c_str (), (* it). length () ;}return true ;}
The source implementation here is actually to read the records in the SQL script by row. If a semicolon is encountered, it indicates that it is already a statement and put it into a container of SQL statements, I don't know if anyone else has a better way. I hope I can talk about it here and leave your code behind.