/*** connect.c ***/ #include<stdio.h> #include<stdlib.h> #include"mysql.h" int main() { MYSQL mysql; int t,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<stdio.h> #include<stdlib.h> #include"mysql.h" int main() { MYSQL* conn_ptr; MYSQL_RES* res_ptr; MYSQL_ROW sqlrow; char taskID[256]; char taskName[256]; int res,i,j; conn_ptr = mysql_init(NULL); if(!conn_ptr) { return EXIT_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); return EXIT_SUCCESS; }
gcc编译:gcc query.c -o query -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
/** insert ***/ #include<stdio.h> #include<stdlib.h> #include"mysql.h" int main(){ MYSQL *conn_ptr; int res; conn_ptr = mysql_init(NULL); if(!conn_ptr){ printf("mysql_init failed!\n"); return EXIT_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); return EXIT_SUCCESS; }
gcc编译:gcc insert.c -o insert -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
/*** delete ***/ #include<stdio.h> #include<stdlib.h> #include"mysql.h" int main(){ MYSQL *conn_ptr; int res; conn_ptr = mysql_init(NULL); if(!conn_ptr){ printf("mysql_init failed!\n"); return EXIT_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); return EXIT_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的库,头文件和附属链接。如何配置网上其他网友写的都已经很详细了。