当前位置:Gxlcms > mysql > MySQL入门之一次函数调用执行多条语句

MySQL入门之一次函数调用执行多条语句

时间:2021-07-01 10:21:17 帮助过:29人阅读

 支持在单个字符串中指定的多语句的执行。要想与给定的连接一起使用该功能,打开连接时,必须将标志参数中的CLIENT_MULTI_STATEMENTS选项指定给mysql_real_connect()。也可以通过调用mysql_set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_ON),为已有的连接设置它。

常用套路:

  1. /* Connect to server with option CLIENT_MULTI_STATEMENTS */
  2. mysql_real_connect(..., CLIENT_MULTI_STATEMENTS);
  3. /* Now execute multiple queries */
  4. mysql_query(mysql,"DROP TABLE IF EXISTS test_table;\
  5. CREATE TABLE test_table(id INT);\
  6. INSERT INTO test_table VALUES(10);\
  7. UPDATE test_table SET id=20 WHERE id=10;\
  8. SELECT * FROM test_table;\
  9. DROP TABLE test_table");
  10. do
  11. {
  12. /* Process all results */
  13. ...
  14. printf("total affected rows: %lld", mysql_affected_rows(mysql));
  15. ...
  16. if (!(result= mysql_store_result(mysql)))
  17. {
  18. printf(stderr, "Got fatal error processing query\n");
  19. exit(1);
  20. }
  21. process_result_set(result); /* client function */
  22. mysql_free_result(result);
  23. } while (!mysql_next_result(mysql));

具体看代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dlfcn.h>
  5. #include <mysql/mysql.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <termios.h>
  12. #include <mysql/mysql.h>
  13. void process_result_set(MYSQL *mysql, MYSQL_RES *result)
  14. {
  15. int i =0;
  16. unsigned int fieldnum;
  17. //从结果集,获取表头信息
  18. MYSQL_FIELD *fields = mysql_fetch_fields(result);
  19. fieldnum = mysql_field_count(mysql);
  20. for (i=0; i<fieldnum; i++)
  21. {
  22. printf("%s\t", fields[i].name);
  23. }
  24. printf("\n");
  25. //从结果集, 按照行获取信息信息
  26. MYSQL_ROW row = NULL;
  27. //从结果集中一行一行的获取数据
  28. while ( row = mysql_fetch_row(result))
  29. {
  30. fieldnum = mysql_field_count(mysql);
  31. //优化,我的行有多少列。。。。查找这样的api函数
  32. for (i=0; i<fieldnum; i++) //经过测试 发现 不是以0结尾的指针数组。。
  33. {
  34. printf("%s\t", row[i]);
  35. }
  36. printf("\n");
  37. }
  38. }
  39. int main()
  40. {
  41. int ret = 0, status = 0;
  42. MYSQL *mysql;
  43. MYSQL_RES *result;
  44. MYSQL_ROW row;
  45. char *query;
  46. mysql = mysql_init(NULL);
  47. mysql =mysql_real_connect(mysql, "localhost", "root", "123456", "mydb2", 0, NULL, CLIENT_MULTI_STATEMENTS);
  48. if (mysql == NULL)
  49. {
  50. ret = mysql_errno(mysql);
  51. printf("func mysql_real_connect() err\n");
  52. return ret;
  53. }
  54. else
  55. {
  56. printf(" ok......\n");
  57. }
  58. /* execute multiple statements */
  59. status = mysql_query(mysql,
  60. "DROP TABLE IF EXISTS test_table;\
  61. CREATE TABLE test_table(id INT);\
  62. INSERT INTO test_table VALUES(10);\
  63. UPDATE test_table SET id=20 WHERE id=10;\
  64. SELECT * FROM test_table;\
  65. DROP TABLE test_table");
  66. if (status)
  67. {
  68. printf("Could not execute statement(s)");
  69. mysql_close(mysql);
  70. exit(0);
  71. }
  72. /* process each statement result */
  73. do {
  74. /* did current statement return data? */
  75. result = mysql_store_result(mysql);
  76. if (result)
  77. {
  78. /* yes; process rows and free the result set */
  79. process_result_set(mysql, result);
  80. mysql_free_result(result);
  81. }
  82. else /* no result set or error */
  83. {
  84. if (mysql_field_count(mysql) == 0)
  85. {
  86. printf("%lld rows affected\n",
  87. mysql_affected_rows(mysql));
  88. }
  89. else /* some error occurred */
  90. {
  91. printf("Could not retrieve result set\n");
  92. break;
  93. }
  94. }
  95. /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
  96. if ((status = mysql_next_result(mysql)) > 0)
  97. printf("Could not execute statement\n");
  98. } while (status == 0);
  99. mysql_close(mysql);
  100. }

以上就是MySQL入门之一次函数调用执行多条语句的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行