How to access the database with mysql+c + API
Editor to share with you mysql+c language + API how to access the database, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
# include # include "CMySQL.h" # include "stdio.h" # define MYSQL_PORT 3306void process_result_set (MYSQL mysql, MYSQL_RES * result); void exit (MYSQL mydata,char * ep); int main (int argc, char * argv []) {MYSQL mydata; Database_Param p; char queryName; MYSQL_RES * result=NULL; int status = 0; / initialize strcpy (p.host, "127.0.0.1"); strcpy (p.user, "root"); strcpy (p.password, "root") Strcpy (p.db, "im"); p.port = MYSQL_PORT; / / initialize the data structure if (mysql_init (& mydata) = = NULL) {printf ("init mysql data stauct fail\ n"); system ("pause"); return-1 } / / Connect if (argc = = 1) {if (NULL = = _ real_connect (& mydata,p.host,p.user,p.password, p.db)) {printf ("connect database fail,%s\ n", mysql_error (& mydata)) {else {printf ("run parameter error\ n"); system ("pause"); return-1 } / / first set the character set strcpy (queryName, "set names gbkd"); if (mysql_query (& mydata, "set names gbk")! = 0) {exit (mydata, "set names fail"); return-1;} / / query data (supports single and multiple fields, output typesetting) strcpy (queryName, "show databases"); if (& mydata,queryName)! = 0) {exit (mydata, "execute sql syntax fail") Return-1;} / / get the query result result= mysql_store_result (& mydata); process_result_set (mydata,result); / / process multiple insert statements, in mysql_real_connect (.) The last argument in the function is changed to: CLIENT_MULTI_STATEMENTS strcpy (queryName, "insert into fangl.admin (username,password) values ('fangl1','fangl1'), (' fangl2','fangl2'), ('fangl3','fangl3');\ insert into fangl.admin (username,password) values (' fl1','fl1'), ('fl2','fl2'), (' fl3','fl3')) \ insert into fangl.admin (username,password) values ('fal1','fal1'), (' fal2','fal2'), ('fal3','fal3');\ select * from fangl.admin "); if (& mydata,queryName)! = 0) {exit (mydata," insert values fail "); return-1;} do {/ * did current statement return data? * / result = mysql_store_result (& mydata); if (result) {/ * yes Process rows and free the result set * / process_result_set (mydata, result); mysql_free_result (result);} else / * no result set or error * / {if (mysql_field_count (& mydata) = = 0) {printf ("% lld rows affected\ n", mysql_affected_rows (& mydata));} else / * some error occurred * / {printf ("Could not retrieve result set\ n"); break }} / * more results?-1 = no, > 0 = error, 0 = yes (keep looping) * / if ((status = mysql_next_result (& mydata)) > 0) printf ("Could not execute statement\ n");} while (status = = 0); / / delete data strcpy (queryName, "delete from fangl.admin where username='fl1'"); if (& mydata,queryName)! = 0) {exit (mydata, "execute sql syntax fail") Return-1;} / query data (support single and multiple fields, output typesetting) strcpy (queryName, "select * from fangl.admin"); if (mysql_query (& mydata,queryName)! = 0) {return-1;} / get query result result= mysql_store_result (& mydata); process_result_set (mydata,result); / / release result if (result! = NULL) {mysql_free_result (result) } mysql_close (& mydata); system ("pause"); return 1;} void process_result_set (MYSQL mydata, MYSQL_RES * result) {int rowcount = mysql_num_rows (result); / / how many rows are there in the result of the query / / get the field names MYSQL_FIELD * fields= NULL; for (int iFields / fields mystical fetching fetched field (result); iSense +) {printf ("% s", fields- > name);} printf ("\ n") / / read each record MYSQL_ROW currow = NULL; while ((currow = mysql_fetch_row (result))! = NULL) {for (int I = 0; I < mysql_num_fields (result); + + I) {printf ("% s", currow [I]? currow [I]: "NULL");} printf ("\ n");} void exit (MYSQL mydata,char * ep) {mysql_close (& mydata) Printf ("% S mydata% s\ n", ep,mysql_error (& mydata)); system ("pause");} above is all the content of this article "mysql+c language + API how to access the database". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!