在Ubuntu体系中,已经安装了mysql,即应用sudo apt-get install mysql-server mysql-client
但是用C编译mysql数据库时,报错
fatal error: mysql.h: No such file or directory
出现这个错误是因为体系没有安装mysql开发库
sudo apt-get install libmysql++-dev
编译时须要加连接-lmysqlclient.
编译源法度的时辰,如下号令:
gcc -I/usr/include/mysql *.c -L/usr/lib/mysql -lmysqlclient -o *
g++ -I/usr/include/mysql csql_demo.cpp -L/usr/lib/mysql -lmysqlclient -o csql_demo.e
and then i got:
$ ./csql_demo.e
Error connecting to Mysql!
change the password and the database name and fixed it now .
/*练习mysql数据库的连接*/
#include <stdio.h>
#include <mysql.h>
int main()
{
MYSQL mysql;
int t, r;
/*连接之前,先用mysql_init初始化MYSQL连接句柄*/
mysql_init(&mysql);
/*使用mysql_real_connect连接服务器,其参数依次为MYSQL句柄,服务器IP地址,
登录mysql的用户名,密码,要连接的数据库等*/
if(!mysql_real_connect(&mysql, "localhost", "root", "123123", "yayaya", 0, NULL, 0))
printf("Error connecting to Mysql!\n");
else
printf("Connected Mysql successful!\n");
/*关闭连接*/
mysql_close(&mysql);
return 0;
}
log in mysql from terminal and
create database yayaya;
and then do this again :
g++ -I/usr/include/mysql csql_demo.cpp -L/usr/lib/mysql -lmysqlclient -o csql_demo.e
./csql_demo.e
Connected Mysql successful!