MySQL入門之一次函數(shù)調(diào)用執(zhí)行多條語(yǔ)句

? 支持在單個(gè)字符串中指定的多語(yǔ)句的執(zhí)行。要想與給定的連接一起使用該功能,打開連接時(shí),必須將標(biāo)志參數(shù)中的client_multi_statements選項(xiàng)指定給mysql_real_connect()。也可以通過(guò)調(diào)用mysql_set_server_option(mysql_option_multi_statements_on),為已有的連接設(shè)置它。

常用套路:

/*?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;  ????????//從結(jié)果集,獲取表頭信息  ????????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入門之一次函數(shù)調(diào)用執(zhí)行多條語(yǔ)句的內(nèi)容,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP中文網(wǎng)(www.php.cn)!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊10 分享