MySQL入門之一次函數調用執行多條語句

? 支持在單個字符串中指定的多語句的執行。要想與給定的連接一起使用該功能,打開連接時,必須將標志參數中的client_multi_statements選項指定給mysql_real_connect()。也可以通過調用mysql_set_server_option(mysql_option_multi_statements_on),為已有的連接設置它。

常用套路:

/*?Connect?to?server?with?option?CLIENT_MULTI_STATEMENTS?*/  mysql_real_connect(...,?CLIENT_MULTI_STATEMENTS);  /*?Now?execute?multiple?queries?*/  mysql_query(mysql,"DROP?TABLE?IF?EXISTS?test_table;  ???????????????????CREATE?TABLE?test_table(id?INT);  ???????????????????INSERT?INTO?test_table?VALUES(10);  ???????????????????UPDATE?test_table?SET?id=20?WHERE?id=10;  ???????????????????SELECT?*?FROM?test_table;  ???????????????????DROP?TABLE?test_table");  do  {  ??/*?Process?all?results?*/  ??...  ??printf("total?affected?rows:?%lld",?mysql_affected_rows(mysql));  ??...  ??if?(!(result=?mysql_store_result(mysql)))  ??{  ?????printf(stderr,?"Got?fatal?error?processing?queryn");  ?????exit(1);  ??}  ??process_result_set(result);?/*?client?function?*/  ??mysql_free_result(result);  }?while?(!mysql_next_result(mysql));

具體看代碼:

#include?<stdio.h>  #include?<stdlib.h>  #include?<string.h>  #include?<dlfcn.h>  #include?<mysql>  #include?<stdio.h>  #include?<stdlib.h>  #include?<unistd.h>  #include?<string.h>  #include?<errno.h>  #include?<termios.h>  #include?<mysql>  void?process_result_set(MYSQL???????*mysql,?MYSQL_RES?*result)  {  ????????int?i?=0;  ????????unsigned?int?fieldnum;  ????????//從結果集,獲取表頭信息  ????????MYSQL_FIELD?*fields?=?mysql_fetch_fields(result);  ????????fieldnum?=?mysql_field_count(mysql);  ????????for?(i=0;?i<fieldnum int status create insert update select drop>0?=?error,?0?=?yes?(keep?looping)?*/  ????????if?((status?=?mysql_next_result(mysql))?&gt;?0)  ????????????????printf("Could?not?execute?statementn");  ????}?while?(status?==?0);  ????mysql_close(mysql);  }</fieldnum></mysql></termios.h></errno.h></string.h></unistd.h></stdlib.h></stdio.h></mysql></dlfcn.h></string.h></stdlib.h></stdio.h>

以上就是MySQL入門之一次函數調用執行多條語句的內容,更多相關內容請關注PHP中文網(www.php.cn)!

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享