Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to import source database in mysql

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to import the source database into mysql. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

You want to have a database script that can be imported without installing a mysql client, but the corresponding api call cannot be found. So you need to implement the method of importing the database by yourself:

Common.h

# ifndef _ COMMON_H#define _ COMMON_H#ifdef WIN32 # include 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# include#endif#include # include# include using namespace std;enum SERVER_ACTION {SERVER_STARTNEW = 0Cindy / start a new service}; # ifdef WIN32#define PATH_DELIMTER "\" # else#define PATH_DELIMTER "/" # endif#endif

Dbmanager.h

# ifndef _ DBMANAGER_H#define _ DBMANAGER_H#include using namespace std;#include "common.h" # include / / 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& sql); bool sourceSql (string fileName); private: MYSQL * mysqlInit (DBInfo & info); void mysqlClose (); private: MYSQL * massively mysqlConnn; DBInfo massively dbConfiguration;}; extern DBManager gadget DBManagertransmissiondif

Dbmanager.cpp

# include "dbmanager.h" # include DBManager gaming DBManagerPlus 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 = 1bot void DBManager::mysqlClose dif return mysql;} void DBManager::mysqlClose () {if (m_mysqlConn) {mysql_close (m_mysqlConn); m_mysqlConn = NULL }} / * / * execute SQL statement * / / * * / 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 database * / / * * * / bool DBManager::SelectDB (string dbName) {if (mysql_select_db (m_mysqlConn) DbName.c_str ()) return false Else return true } / * / / * fileName is the path of the sql file, parsing every sql statement in fileName Put it into the sql container * / / * / bool DBManager::readFromSql (string fileName,vector& 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) {/ / found the end bit of a statement sql.push_back (signalSql); signalSql.clear ();} s.clear ();} in.close (); return true } / * / / * Import database sql * / / * * / bool DBManager::sourceSql (string fileName) {vector vecSql Bool ret = readFromSql (fileName,vecSql); if (ret = = false) {printf ("failed to import gamedb.sql"); return false;} for (vector::iterator it = vecSql.begin (); it! = vecSql.end (); it++) {ExeSql ((* it). C_str (), (* it). Length ()) } return true;} the above is how to import the source database into the mysql shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report