Lors de l'apprentissage de la base de données, nous devons connaître quelques applications simples, telles que l'application simple mysql api, des amis qui l'aiment. peut jeter un oeil.
#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 %s\n",mysql_error(&mysql)); exit(1); } sprintf(qbuf,"select id,username,groupname from usergroup;"); if(mysql_query(sock,qbuf)){ fprintf(stderr,"query error %s\n",mysql_error(sock)); exit(1); } if(!(res = mysql_store_result(sock))) { exit(1); } printf("number of fields returned :%d\n",mysql_num_fields(res)); int i=0; while((row = mysql_fetch_row(res)) != NULL) { printf("%s,%s,%s\n",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; }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!