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*'


然后就可以进行编译
sudo g++ -o target main.cpp -L~/usr/lib/x86_64-linux-gnu -lMysqLclient

测试代码:
#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;
}

结果:





相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...