一、安装mysql
1、sudo apt-get intsall mysql-server
2、sudo apt-get install mysql-client
3、sudo apt-get install libmysqlclient-dev
二、检查一下mysql
1、sudo netstat -tap|grep mysql;
2、采用mysql登陆一下
3、查看当前mysql服务状况
service mysql status
4、查看mysql库的存放位置mysql_config --libs
5、查看mysql头文件存放位置mysql_config --cflags
三、不错的参考网址
http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html
http://www.cyberciti.biz/files/mysql-c-api.c.txt
http://dev.mysql.com/doc/refman/5.0/en/c.html
http://pkgs.org/download/libmysqlclient.so.16()(64bit)
四、写一个测试程序
create database test;create table person(id int,name char(20));
#include <stdio.h>
#include <mysql/mysql.h>
int main()
{
MYSQL mysql_conn;
MYSQL_RES* mysql_result;
MYSQL_ROW mysql_row;
int f1,f2,num_row,num_col;
if(mysql_init(&mysql_conn) == NULL)
{
printf("init fails\n");
return 0;
}
if(mysql_real_connect(&mysql_conn,"localhost","root",
"123456","test",MYSQL_PORT,NULL,0) == NULL)
{
printf("connect fails\n");
return 0;
}
//mysql_query(&mysql_conn,"set names 'utf8'");
if(mysql_query(&mysql_conn,"select * from person") == 0)
{
mysql_result = mysql_store_result(&mysql_conn);
num_row = mysql_num_rows(mysql_result);
num_col = mysql_num_fields(mysql_result);
printf("ROW:%d,col:%d\n",num_row,num_col);
for(f1 = 0;f1 < num_row;f1++)
{
mysql_row = mysql_fetch_row(mysql_result);
for(f2 = 0; f2 < num_col;f2++)
{
printf("row:%d,col%d==>[%s]\n",f1,f2,mysql_row[f2]);
}
}
}
else
{
printf("query fails\n");
}
mysql_free_result(mysql_result);
mysql_close(&mysql_conn);
printf("quit\n");
return 0;
}
测试结果
ROW:1,col:2
row:0,col0==>[1]
row:0,col1==>[steven]
quit