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

創(chuàng)新互聯(lián)建站成立于2013年,我們提供高端成都網(wǎng)站建設(shè)、成都網(wǎng)站制作公司、成都網(wǎng)站設(shè)計(jì)公司、網(wǎng)站定制、成都營(yíng)銷網(wǎng)站建設(shè)、小程序設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、seo優(yōu)化服務(wù),提供專業(yè)營(yíng)銷思路、內(nèi)容策劃、視覺(jué)設(shè)計(jì)、程序開(kāi)發(fā)來(lái)完成項(xiàng)目落地,為成都公路鉆孔機(jī)企業(yè)提供源源不斷的流量和訂單咨詢。
想有一個(gè)不需要安裝mysql客戶端就可以導(dǎo)入數(shù)據(jù)庫(kù)腳本,但找不到對(duì)應(yīng)的api調(diào)用。所以得需要自己去實(shí)現(xiàn)導(dǎo)入數(shù)據(jù)庫(kù)的實(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,//開(kāi)新服
};
#ifdef WIN32
#define PATH_DELIMTER "\\"
#else
#define PATH_DELIMTER "/"
#endif
#endifdbmanager.h
#ifndef _DBMANAGER_H
#define _DBMANAGER_H
#include <string>
using namespace std;
#include "common.h"
#include <mysql.h>
//數(shù)據(jù)庫(kù)配置信息
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;#endifdbmanager.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語(yǔ)句 */
/************************************************************************/
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ù)庫(kù) */
/************************************************************************/
bool DBManager::SelectDB(string dbName)
{
if(mysql_select_db(m_mysqlConn,dbName.c_str()))
return false;
else
return true;
}
/************************************************************************/
/*fileName是sql文件的路徑, 解析出fileName中的每一條sql語(yǔ)句,放入到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){//找到了一條語(yǔ)句的結(jié)束位
sql.push_back(signalSql);
signalSql.clear();
}
s.clear();
}
in.close();
return true;
}
/************************************************************************/
/* 導(dǎo)入數(shù)據(jù)庫(kù)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ù)庫(kù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:mysql中怎么導(dǎo)入source數(shù)據(jù)庫(kù)
網(wǎng)頁(yè)鏈接:http://chinadenli.net/article22/ihjpcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、ChatGPT、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、虛擬主機(jī)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)