之前有好多人在為這件事情頭疼不已:
想有一個不需要安裝mysql客戶端就可以導入數據庫腳本,但找不到對應的api調用。所以得需要自己去實現導入數據庫的實現方法:
common.h
#ifndef _COMMON_H #define _COMMON_H #ifdef WIN32 #include <winsock2.h> typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; #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,//開新服 }; #ifdef WIN32 #define PATH_DELIMTER "\\" #else #define PATH_DELIMTER "/" #endif #endif
#ifndef _DBMANAGER_H #define _DBMANAGER_H #include <string> using namespace std; #include "common.h" #include <mysql.h> //數據庫配置信息 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
#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 >= 50013 my_bool reconnect = 1; if (mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect)) { int ret = mysql_errno(mysql); mysql_close(mysql); return NULL; } #else mysql->reconnect = 1; #endif return mysql; } void DBManager::mysqlClose() { if (m_mysqlConn) { mysql_close(m_mysqlConn); m_mysqlConn = NULL; } } /************************************************************************/ /* 執行SQL語句 */ /************************************************************************/ 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; } /************************************************************************/ /* 選擇數據庫 */ /************************************************************************/ bool DBManager::SelectDB(string dbName) { if(mysql_select_db(m_mysqlConn,dbName.c_str())) return false; else return true; } /************************************************************************/ /*fileName是sql文件的路徑, 解析出fileName中的每一條sql語句,放入到sql容器中 */ /************************************************************************/ bool DBManager::readFromSql(string fileName,vector<string>& sql){ ifstream in(fileName.c_str(), ios::in);//linux string signalSql,s; if(!in){ return false; } while(getline(in,s)){ int pos = s.find(";"); signalSql += s; if(pos != s.npos){//找到了一條語句的結束位 sql.push_back(signalSql); signalSql.clear(); } s.clear(); } in.close(); return true; } /************************************************************************/ /* 導入數據庫sql */ /************************************************************************/ bool DBManager::sourceSql(string fileName){ vector<string> vecSql; bool ret = readFromSql(fileName,vecSql); if(ret == false){ printf("導入gamedb.sql失敗"); return false; } for (vector<string>::iterator it = vecSql.begin(); it != vecSql.end(); it++) { ExeSql((*it).c_str(), (*it).length()); } return true; }