linux下用c连接mysql
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
int main(void)
{
MYSQL * sql_p;
MYSQL_RES * result_p;
MYSQL_ROW row_p;
int res;
sql_p = mysql_init(NULL);
if(!sql_p){
printf("Init error!\n");
exit(1);
}
MYSQL * conn = mysql_real_connect(sql_p,"host","user","pwd","test",3306,NULL,0);
if(!conn){
printf("error,%d:%s\n",mysql_errno(sql_p),mysql_error(sql_p));
exit(1);
}
res=mysql_query(sql_p,"select * from user");
if(res){
printf("Query error!\n");
exit(1);
}
result_p = mysql_use_result(sql_p);
if(!result_p){
printf("mysql use error");
}
while((row_p=mysql_fetch_row(result_p))){
printf("row:%s,%s,%s\n",row_p[0],row_p[1],row_p[2]);
}
mysql_close(sql_p);
}
保存为mysql.c
编译命令:gcc -o mysql mysql.c -I/usr/include/mysql -lmysqlclient