轉帖|其它|編輯:郝浩|2009-01-09 11:34:41.000|閱讀 1215 次
概述:C++語言代碼在Linux下操作MySQL數據庫
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
文章關鍵字:|Linux|C++|語言|代碼|操作|MySQL|數據庫|
代碼:
#include
#include
#include "/usr/local/include/mysql/mysql.h" //安裝的mysql的頭文件所在的位置
using namespace std;
string host="數據庫地址";
string user="用戶名";
string pwd="密碼";
string dbname="數據庫";
string sql="查詢語句";
unsigned int port=3309;#端口號
int status;
int main(){
MYSQL *mysql;
mysql=mysql_init(0);
MYSQL_RES *result;
MYSQL_ROW row;
if(mysql_real_connect(mysql,host.c_str(),user.c_str(),pwd.c_str(),dbname.c_str(),port,NULL,CLIENT_FOUND_ROWS)==NULL){
cout << "connect failure!" << endl;
return EXIT_FAILURE;
}else{
cout << "connect success!" << endl;
}
mysql_set_character_set(mysql,"gbk");
status=mysql_query(mysql,sql.c_str());
if(status !=0 ){
cout << "query failure!" << endl;
}
cout << "the status is :" << status << endl;
result=mysql_store_result(mysql);
while(row=mysql_fetch_row(result)){
cout << row[1] <<"|"<< row[2] << endl;
}
mysql_free_result(result);
mysql_close(mysql);
}
編譯:
g++ -o test test.cpp -lmysqlclient -I/usr/local/include/mysql/ -L/usr/local/lib/mysql
后面的那些用來指定mysql安裝的時候包含路徑和庫文件路徑,具體與你機器上的mysql安裝的路徑有關。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:賽迪網