mysql資料庫是一種關係型資料庫, 近年來的使用者迅速增加,下面介紹幾種c++連線mysql的方式 .h
#ifndef mysqldatabase_mysqldatabase_h_
#define mysqldatabase_mysqldatabase_h_
#include
#include
#include
#include
#include
#include
"mysql.h"
class
mysqldatabase
;#endif
.cpp
#include
"mysqloperation.h"
#include
"logger.h"
#include
#include
mysqldatabase::
mysqldatabase()
void mysqldatabase::
setconfiginfo
(std::string db_ip,
int db_port, std::string db_name, std::string db_user, std::string db_password)
bool mysqldatabase::
connectdatabase()
if(con !=
null
&&mysql_real_connect
(con, db_ip_.
c_str()
, db_user_.
c_str()
, db_password_.
c_str()
, db_name_.
c_str()
,(unsigned
)db_port_/*3306 tcp ip埠*/
,null
/*unix socket 連線型別*/
, client_multi_statements/*執行成odbc資料庫標誌*/))
} std::thread th
(std::
bind
(&mysqldatabase::operationdatabaseloop,
this))
; th.
detach()
; connect_status_ =
true;}
else
return connect_status_;
}void mysqldatabase::
closedatabase()
mysql_close
(con);}
void mysqldatabase::
operationdatabaseloop()
else}}
else
}sleep
(100);
}}bool mysqldatabase::
operationdatabase
(std::vectoroperations)
return
true;}
bool mysqldatabase::
operationdatabase
(std::vectoroperations,
int& index)
//用於初始化時插入資料庫失敗返回錯誤語句
return
true;}
mysql_res * mysqldatabase::
selectdatabase
(std::string select)
std::lock_guard<
decltype
(mutex_opt_)
>
lock
(mutex_opt_)
;bool select_status =
false
;int rt =
mysql_real_query
(con, select.
c_str()
,strlen
(select.
c_str()
));if
(rt)
else
//return select_status;
}bool mysqldatabase::
freeselect
(mysql_res *result)
int mysqldatabase::
getsqloptsize()
bool mysqldatabase::
operationdatabase
(std::string operation)
bool operation_status =
false
;int rt =
mysql_query
(con, operation.
c_str()
);if(rt !=0)
else
return operation_status;
}
qt +
= sql
//如下**段為連線mysql資料庫
qsqldatabase db = qsqldatabase::
adddatabase
("qmysql");
db.sethostname
("127.0.0.1");
db.setdatabasename
("tradesystem");
db.setport
(3306);
db.setusername
("root");
db.setpassword
("123456");
bool ok = db.
open()
;
//對資料庫的增刪查改
//1、查詢
qsqlquery query;
bool b=query.
exec
(selectorderssql)
;//填入sql語句
while
(query.
next()
)//2、非查詢
qsqlquery query;
qstring insertsql =
"delete from weatherinfo where weather_id = %1"
; qstring sql = insertsql.
arg(5)
;bool b=query.
exec
(sql);if
(b)else
udl 連mysql 幾種常見的資料庫連線方法
連線access資料庫 1.使用已有dsn的連線字串進行連線 odbc 使用dsn進行連線 匯入命名空間usingsystem.data.odbc protectedvoidpage load object sender,eventargs e ctionstring屬性為provider 提供程式...
資料庫幾種連線
使用 關係將表連線起來的查詢,其查詢結果中列出被連線表中的所有列,包括其中的重複列。select xsb.cjb.from xsb cjb where xsb.學號 cjb.學號資料庫應用中最常用的是 自然連線 它在目標列中去除相同的欄位名。select xsb.cjb.課程號,cjb.成績 fro...
jdbc連線mysql資料庫的幾種方式
第一種 driver driver newdriver string url jdbc mysql localhost 3306 aa?servertimezone utc properties pro newproperties pro.setproperty user root pro.setp...