溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql中怎么導(dǎo)入source數(shù)據(jù)庫

發(fā)布時(shí)間:2021-08-03 16:47:10 來源:億速云 閱讀:202 作者:Leah 欄目:數(shù)據(jù)庫

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)mysql中怎么導(dǎo)入source數(shù)據(jù)庫,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

想有一個(gè)不需要安裝mysql客戶端就可以導(dǎo)入數(shù)據(jù)庫腳本,但找不到對應(yīng)的api調(diào)用。所以得需要自己去實(shí)現(xiàn)導(dǎo)入數(shù)據(jù)庫的實(shí)現(xiàn)方法:

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>
//數(shù)據(jù)庫配置信息
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;
	}
} 
/************************************************************************/
/* 執(zhí)行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;
}
/************************************************************************/
/* 選擇數(shù)據(jù)庫                                                           */
/************************************************************************/
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){//找到了一條語句的結(jié)束位
			sql.push_back(signalSql);
			signalSql.clear();
		}
		s.clear();
	}
	in.close();
	return true;
}
/************************************************************************/
/* 導(dǎo)入數(shù)據(jù)庫sql                                                        */
/************************************************************************/
bool DBManager::sourceSql(string fileName){
	vector<string> vecSql;
	bool ret = readFromSql(fileName,vecSql);
	if(ret == false){
		printf("導(dǎo)入gamedb.sql失敗");
		return false;
	}
	for (vector<string>::iterator it = vecSql.begin(); it != vecSql.end(); it++)
	{
		ExeSql((*it).c_str(), (*it).length());
	}
	return true;
}

上述就是小編為大家分享的mysql中怎么導(dǎo)入source數(shù)據(jù)庫了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI