在學習數據庫的時候,我們需要了解一些簡單的應用,比如mysql api簡單應用,喜歡的小伙伴們可以看一下。
#include?<stdio.h> #include?<stdlib.h> #include?"mysql.h" int?insert_new_table(MYSQL?*sock1,const?char?*row1,const?char?*row2) { char?buf[128]; sprintf(buf,"insert?into?aaa.tmp?(num,name)?VALUES(%s,'%s')",row1,row2); mysql_query(sock1,buf); printf("----n"); ????return?0; } int?main(int?argc,char?**argv) { ????????MYSQL?mysql,*sock; ????????MYSQL_RES?*res; MYSQL_FIELD?*fd; MYSQL_ROW?row; char?qbuf[160]; //init?mysql ????????mysql_init(&mysql); sock?=?mysql_real_connect(&mysql,"localhost","root","root","tmp",0,NULL,0); if(sock?==?0) { ????fprintf(stderr,"connect?mysql?db?%sn",mysql_error(&mysql)); ????????exit(1); } sprintf(qbuf,"select?id,username,groupname?from?usergroup;"); if(mysql_query(sock,qbuf)){ ?????fprintf(stderr,"query?error?%sn",mysql_error(sock)); ?????????exit(1); ?} ? ?if(!(res?=?mysql_store_result(sock))) ?{ ?????exit(1); ?} ?????????printf("number?of?fields?returned?:%dn",mysql_num_fields(res)); ?int?i=0; ?while((row?=?mysql_fetch_row(res))?!=?NULL) ?{ ?????printf("%s,%s,%sn",row[i],row[i+1],row[i+2]); //???????????insert_new_table(sock,row[i],row[i+1]);???????//insert ?} ?mysql_free_result(res); ?mysql_close(sock); ?return?0; }</stdlib.h></stdio.h>
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END