当前位置:Gxlcms > mysql > Linux下MySQLC++连接操作

Linux下MySQLC++连接操作

时间:2021-07-01 10:21:17 帮助过:18人阅读

1,客户端需要安装mysql-client包,安装完后有我们需要的库文件;2,然后安装mysql-dev包,安装完后有我们需要的mysql.h文件;3,作

1,客户端需要安装mysql-client包,安装完后有我们需要的库文件;

2,然后安装mysql-dev包,安装完后有我们需要的mysql.h文件;

3,作为客户端只需要安装这两个包就可以了:

以下是简单的测试代码:
#include
#include
int main()
{
/*declare structures and variables */
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
//initialize MYSQL structure
mysql_init(&mysql);
//connect to database
// mysql_real_connect(&mysql,"localhost","root","nriet","test",0,NULL,0);
mysql_real_connect(&mysql,"192.168.13.27","root","nriet","nriet",0,NULL,0);
//execute query
mysql_query(&mysql,"select * from test");
//get result set
result=mysql_store_result(&mysql);
//process result set
while((row=mysql_fetch_row(result)))
{
fprintf(stdout,"%s - %s\n",row[0],row[1]);
}
//mysql_query(&mysql,"insert into test values(3,'a')");
//clean up
mysql_free_result(result);
mysql_close(&mysql);
}

4,在“GCC C++ Linker”下的“Libraries”:

libraries(-l):mysqlclient
Libraries search path(-L):/usr/lib/mysql
然后 就可以 大功告成了 !!

linux

人气教程排行