When learning databases, we need to understand some simple applications, such as mysql api simple applications, friends who like We can take a look.
#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; }
The above is the detailed content of what is mysql c api? Parse mysql c api simple application. For more information, please follow other related articles on the PHP Chinese website!