这期内容当中小编将会给大家带来有关mysql中怎么导入source数据库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
想有一个不需要安装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
dbmanager.h
#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
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 >= 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;
}
}
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;
}
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;
}
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;
}
上述就是小编为大家分享的mysql中怎么导入source数据库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。