准备工作:
安装:
#yum install -y mysql*
启动
#service mysqld start
1、创建数据类型
create table test(id int, name char(20) ); insert into test values(0,"beijing"); insert into test values(1,"shanghai"); insert into test values(2,"chongqin"); insert into test values(3,"dongjing"); insert into test values(4,"guiyang"); insert into test values(5,"najing"); insert into test values(6,"guangzhou"); insert into test values(7,"shengzhen"); insert into test values(8,"taibei"); insert into test values(9,"tianjing"); insert into test values(10,"xian"); insert into test values(11,"changsha");
2、demo.c 程序
#include <mysql.h> #include <stdio.h> void pthread_DB() { MYSQL mysql; MYSQL_RES *res = NULL; MYSQL_ROW row; char x_sql[1000]="\0"; int Id[2]; char Name[10][20]; char Default[10][108]; mysql_init(&mysql); if(!mysql_real_connect(&mysql,"localhost","root","","dbname",0,NULL,0)) { printf("error connecting to database:%s\n",mysql_error(&mysql)); } sprintf(x_sql,"select id,name from test"); printf("x_sql=%s\t\n",x_sql); if((res = mysql_query(&mysql,x_sql))){ printf("query error: %s\t\n",res); } else{ res = mysql_store_result(&mysql); while((row = mysql_fetch_row(res))) { printf("id = %3s,",row[0]); printf("Name = %s \n",row[1]); fflush(stdout); } mysql_free_result(res); } mysql_close(&mysql); } void main(){ pthread_DB(); }
2、编译
gcc -o demo demo.c -I /usr/include/mysql -L /usr/lib/mysql -l mysqlclient -lz