• MYSQL C API : CLIENT_MULTI_STATEMENTS 选项


      1 #include <iostream>
      2 #include <mysql.h>
      3 #include <string>
      4 
      5 #include <assert.h>
      6 
      7 
      8 
      9 int main()
     10 {
     11     MYSQL *ms_conn = mysql_init(NULL);
     12     if (ms_conn == NULL)
     13     {
     14         std::cout << "Error: mysql_init failed." << std::endl;
     15         return 0;
     16     }
     17     std::cout << "Info: mysql_init success." << std::endl;
     18 
     19     // 参数CLIENT_MULTI_STATEMENTS 指定一次query 可以执行多条SQL
     20     MYSQL *ms_temp = NULL;
     21     ms_temp = mysql_real_connect(ms_conn, "localhost", "root", "123456sx", 
     22             "suyh", 0, NULL, CLIENT_MULTI_STATEMENTS);
     23     if (ms_temp == NULL)
     24     {
     25         std::cout << "Error: mysql_real_connect() failed." << std::endl;
     26         std::cout << mysql_error(ms_conn) << std::endl;
     27         mysql_close(ms_conn), ms_conn = NULL;
     28         return 0;
     29     }
     30     std::cout << "Info: mysql_real_connect() succect." << std::endl;
     31 
     32     std::string str_sqls = "";
     33     str_sqls += "SELECT id, account, sex, level, powers FROM player_data WHERE id = 100000";
     34     str_sqls += ";";
     35     str_sqls += "SELECT id, account, sex, level, powers FROM player_data WHERE id = 100001";
     36     str_sqls += ";";
     37     str_sqls += "SELECT id, account, sex, level, powers FROM player_data WHERE id = 100002";
     38 
     39     int res = 0;
     40     res = mysql_real_query(ms_conn, str_sqls.c_str(), str_sqls.size());
     41     if (res != 0)
     42     {
     43         std::cout << "Info: query failed, sql: " << str_sqls.c_str() << std::endl;
     44         std::cout << mysql_error(ms_conn) << std::endl;
     45     }
     46     else
     47     {
     48         std::cout << "Info: query success." << std::endl;
     49         std::cout << std::endl;
     50 
     51         // CLIENT_MULTI_STATEMENTS 指定该选项需要mysql_next_result() 函数来获取下一条结果集
     52         // 如果结果集未取完,则不能进行query 操作。
     53         do
     54         {
     55             MYSQL_RES *ms_res = mysql_store_result(ms_conn);
     56             assert(ms_res != NULL);
     57 
     58             unsigned int field_num = mysql_num_fields(ms_res);
     59             std::cout << "fileds number is " << field_num << std::endl;
     60 
     61             MYSQL_FIELD* field_info = mysql_fetch_field(ms_res);
     62             assert(field_info != NULL);
     63 
     64             MYSQL_ROW row_data = NULL;
     65             while (1)
     66             {
     67                 row_data = mysql_fetch_row(ms_res);
     68                 if (row_data == NULL)
     69                     break;
     70 
     71                 unsigned long *field_lens = mysql_fetch_lengths(ms_res);
     72                 assert(field_lens != NULL);
     73                 for (int i = 0; i < field_num; ++i)
     74                 {
     75                     if (field_info[i].type == MYSQL_TYPE_BLOB)
     76                     {
     77                         std::cout << "current filed type is BLOB." << std::endl;
     78                         continue;
     79                     }
     80 
     81                     if (row_data[i] == NULL)
     82                     {
     83                         std::cout << "field_lens[" << i << "] = " << field_lens[i]
     84                             << ", value is NULL." << std::endl;
     85                     }
     86                     else
     87                     {
     88                         std::cout << "field_lens[" << i << "] = " << field_lens[i] 
     89                             << ", value is " << row_data[i] << std::endl;
     90                     }
     91                 }
     92             }
     93 
     94             mysql_free_result(ms_res), ms_res = NULL;
     95 
     96             std::cout << "#######################################" << std::endl;
     97         } while (mysql_next_result(ms_conn) == 0);
     98     }
     99 
    100 
    101 
    102     mysql_close(ms_conn), ms_conn = NULL;
    103     return 0;
    104 }
  • 相关阅读:
    弹性盒模型
    CSS3属性
    CSS3选择器
    闭包
    angularjs-select2的使用
    angular 分页插件的使用
    webstorm 破解
    数组和字符串之间的转化
    git 拉取分支代码 合分支
    时间戳转化为时间格式 时间格式转为时间戳
  • 原文地址:https://www.cnblogs.com/suyunhong/p/4789024.html
Copyright © 2020-2023  润新知