C语言操作mysql 添加、删除、修改、查询实例

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h> int main (int argc, char *argv[]){ MYSQL *conn_ptr; MYSQL_RES *res; MYSQL_ROW row; char query[1024]; int t,r; conn_ptr=mysql_init(NULL); if(!conn_ptr){ fprintf(stderr, "mysql_init_failed/n"); return EXIT_FAILURE; } conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","123456","test",0,NULL,0); if( conn_ptr ){ printf("Connection success/n"); }else{ printf("Error connecting to database: %s/n", mysql_error(conn_ptr)); } //mysql_query(conn_ptr, "insert into pay(id,uid,name)values(201104010010,134,'test123')"); //mysql_query(conn_ptr, "delete from pay where id = 201104010010"); mysql_query(conn_ptr, "update pay set name='test_test' where id = 201104010002"); sprintf(query, "select * from pay order by id desc limit 2"); t=mysql_query(conn_ptr,query); if(t) { printf("Error making query:%s/n", mysql_error(conn_ptr)); } else printf("Query made... /n"); res=mysql_use_result(conn_ptr); for(r=0;r<mysql_field_count(conn_ptr); r++){ row=mysql_fetch_row(res); if(row<0) break; for(t=0;t<mysql_num_fields(res);t++) printf("%s ",row[t]); printf("/n"); } mysql_close(conn_ptr); return EXIT_SUCCESS; }

你可能感兴趣的:(c,mysql,database,query,语言,include)