/***
connect.c
***/#include#include#include"mysql.h"
intmain()
{
MYSQL mysql;intt,r;
mysql_init(&mysql);if(!mysql_real_connect(&mysql,"localhost","root","mysql123","csql",0,NULL,0))
{
printf("Error connecting to database:%s\n",mysql_error(&mysql));
}else{
printf("Connect MYSQL successfully!\n");
}
mysql_close(&mysql);return 0;
}
gcc编译:gcc connect.c -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
/***
query
***/#include#include#include"mysql.h"
intmain()
{
MYSQL*conn_ptr;
MYSQL_RES*res_ptr;
MYSQL_ROW sqlrow;char taskID[256];char taskName[256];intres,i,j;
conn_ptr=mysql_init(NULL);if(!conn_ptr)
{returnEXIT_FAILURE;
}
conn_ptr= mysql_real_connect(conn_ptr,"localhost","root","mysql123","csql",0,NULL,0);if(conn_ptr)
{
res= mysql_query(conn_ptr,"select * from task where taskState=4 order by taskLastEditTime desc");if(res)
{
printf("SELECT error:%s\n",mysql_error(conn_ptr));
}else{
res_ptr=mysql_store_result(conn_ptr);if(res_ptr)
{
printf("%lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));
j=mysql_num_fields(res_ptr);while((sqlrow =mysql_fetch_row(res_ptr)))
{for(i = 0 ; i < j; i++)
{
printf("%s\t",sqlrow[i]);
}
printf("\n");break;
}if(mysql_errno(conn_ptr))
{
fprintf(stderr,"Receive error:%s",mysql_error(conn_ptr));
}
}
mysql_free_result(res_ptr);
}if(res)
{
printf("mysql_insert error:%s\n",mysql_error(conn_ptr));
}
}else{
printf("Connection failed\n");
}
mysql_close(conn_ptr);returnEXIT_SUCCESS;
}
gcc编译:gcc query.c -o query -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
/**
insert
***/#include#include#include"mysql.h"
intmain(){
MYSQL*conn_ptr;intres;
conn_ptr=mysql_init(NULL);if(!conn_ptr){
printf("mysql_init failed!\n");returnEXIT_FAILURE;
}
conn_ptr= mysql_real_connect(conn_ptr,"localhost","root","shenhaoinfo","csql",0,NULL,0);if(conn_ptr){
res= mysql_query(conn_ptr,"insert into children values(null,'jesffsui',5)");// if(!res){
printf("Insert %lu rows\n",(unsigned long)mysql_affected_rows(conn_ptr));
}else{
fprintf(stderr,"insert error %d: %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr));
}
}else{
printf("Connection failed\n");
}
mysql_close(conn_ptr);returnEXIT_SUCCESS;
}
gcc编译:gcc insert.c -o insert -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
/***
delete
***/#include#include#include"mysql.h"
intmain(){
MYSQL*conn_ptr;intres;
conn_ptr=mysql_init(NULL);if(!conn_ptr){
printf("mysql_init failed!\n");returnEXIT_FAILURE;
}
conn_ptr= mysql_real_connect(conn_ptr,"localhost","root","shenhaoinfo","csql",0,NULL,0);if(conn_ptr){//res = mysql_query(conn_ptr,"insert into children values(null,'jesffsui',5)");
res =mysql_query(conn_ptr,"delete from children where fname = 'jesffsui'");if(!res){
printf("delete %lu rows\n",(unsigned long)mysql_affected_rows(conn_ptr));
}else{
fprintf(stderr,"delete error %d: %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr));
}
}else{
printf("Connection failed\n");
}
mysql_close(conn_ptr);returnEXIT_SUCCESS;
}
gcc编译:gcc delete.c -o delete -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
分别是大写的i,表示INCLUDE,大写的L,表示LINK,小写的L,表示link 一个库
无论是windows环境下还是linux环境下,编译链接都要带上mysql的库,头文件和附属链接。如何配置网上其他网友写的都已经很详细了。