前端之家收集整理的这篇文章主要介绍了
ubuntu下c++链接数据库,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先卸载MysqL,因为在用c++链接库时一直找不到,感觉是哪里弄错,所以想要整个卸载重新安装命令如下:
sudo apt-get remove MysqL-*
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
接着我们要重新安装MysqL,以及
devel开发包,这是在linux下进行数据库开发需要装的,命令如下:
sudo apt-get install MysqL-server
sudo apt-get install libMysqLd-dev
apt-get install MysqL-client
apt-get install libMysqLclient-dev
然后就可以在/usr/include/MysqL路径下找到MysqL.h这个库了,然后include时要用MysqL/MysqL.h
运行时需要链接
libMysqLclient.so,找不到的话可以用下面命令
,记得加sudo不然很多路径没权限访问,就可以看到了
sudo find / -name '*libMysqL*'
@H_
502_62@
然后就可以进行编译
#include <iostream>
#include <MysqL/MysqL.h>
using namespace std;
int main()
{
MysqL_RES *result;
MysqL_ROW row;
MysqL *connection;
MysqL MysqL;
int state;
MysqL_init(&MysqL);
connection = MysqL_real_connect(&MysqL,"ip","db","pw","tb",0);
if(connection == NULL){
cout << MysqL_error(&MysqL) << endl;
return 0;
}
else cout << "connect successfully" << endl;
state = MysqL_query(connection,"select ip,group_id from ip_group");
if(state != 0){
cout << MysqL_error(connection) << endl;
return 0;
}
else cout << "connect successfully" << endl;
}
结果: