支持在单个字符串中指定的多语句的执行。要想与给定的连接一起使用该功能,打开连接时,必须将标志参数中的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)) > 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