zoukankan      html  css  js  c++  java
  • MYSQL C API : mysql_real_query()

     1 enum enum_field_types { 
     2     MYSQL_TYPE_DECIMAL, 
     3     MYSQL_TYPE_TINY,
     4     MYSQL_TYPE_SHORT,  
     5     MYSQL_TYPE_LONG,
     6     MYSQL_TYPE_FLOAT,  
     7     MYSQL_TYPE_DOUBLE,
     8     MYSQL_TYPE_NULL,   
     9     MYSQL_TYPE_TIMESTAMP,
    10     MYSQL_TYPE_LONGLONG,
    11     MYSQL_TYPE_INT24,
    12     MYSQL_TYPE_DATE,   
    13     MYSQL_TYPE_TIME,
    14     MYSQL_TYPE_DATETIME, 
    15     MYSQL_TYPE_YEAR,
    16     MYSQL_TYPE_NEWDATE, 
    17     MYSQL_TYPE_VARCHAR,
    18     MYSQL_TYPE_BIT,
    19     MYSQL_TYPE_TIMESTAMP2,
    20     MYSQL_TYPE_DATETIME2,
    21     MYSQL_TYPE_TIME2,
    22     MYSQL_TYPE_NEWDECIMAL=246,
    23     MYSQL_TYPE_ENUM=247,
    24     MYSQL_TYPE_SET=248,
    25     MYSQL_TYPE_TINY_BLOB=249,
    26     MYSQL_TYPE_MEDIUM_BLOB=250,
    27     MYSQL_TYPE_LONG_BLOB=251,
    28     MYSQL_TYPE_BLOB=252,
    29     MYSQL_TYPE_VAR_STRING=253,
    30     MYSQL_TYPE_STRING=254,
    31     MYSQL_TYPE_GEOMETRY=255
    32 }
    33 
    34 typedef struct st_mysql_field {
    35   char *name;                 /* Name of column 列名 */
    36   char *org_name;             /* Original column name, if an alias */
    37   char *table;                /* Table of column if column was a field */
    38   char *org_table;            /* Org table name, if table was an alias */
    39   char *db;                   /* Database for table */
    40   char *catalog;           /* Catalog for table */
    41   char *def;                  /* Default value (set by mysql_list_fields) */
    42   unsigned long length;       /* Width of column (create length) */
    43   unsigned long max_length;   /* Max width for selected set */
    44   unsigned int name_length;
    45   unsigned int org_name_length;
    46   unsigned int table_length;
    47   unsigned int org_table_length;
    48   unsigned int db_length;
    49   unsigned int catalog_length;
    50   unsigned int def_length;
    51   unsigned int flags;         /* Div flags */
    52   unsigned int decimals;      /* Number of decimals in field */
    53   unsigned int charsetnr;     /* Character set */
    54   enum enum_field_types type; /* Type of field. See mysql_com.h for types */   // 字段的mysql 数据类型
    55   void *extension;
    56 } MYSQL_FIELD;
    57 
    58 
    59 int    mysql_real_query(MYSQL *mysql, const char *q, unsigned long length);
    60     说明:执行SQL
    61     参数1:已初始化的MYSQL 实例;
    62     参数2:SQL 语句;
    63     参数3:SQL 语句字符数。
    64 
    65 MYSQL_RES *mysql_store_result(MYSQL *mysql);
    66     说明:对于成功检索了数据的每个查询(SELECT、SHOW、DESCRIBE、EXPLAIN、CHECK TABLE等),必须调用mysql_store_result()或mysql_use_result()
    67     参数:已初始化的MYSQL 实例。
    68 
    69 // typedef char **MYSQL_ROW; 
    70 MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
    71     说明:检索一个结果集合的下一行。当在mysql_store_result()之后使用时,如果没有更多的行可检索时,mysql_fetch_row()返回NULL。当在mysql_use_result()之后使用时,当没有更多的行可检索时或如果出现一个错误,mysql_fetch_row()返回NULL。

    代码范例:

     1 #include <iostream>
     2 #include <mysql.h>
     3 #include <string>
     4 
     5 #include <assert.h>
     6 
     7 int main()
     8 {
     9     // 1、初始化MYSQL 实例
    10     MYSQL *ms_conn = mysql_init(NULL);
    11     if (ms_conn == NULL)
    12     {
    13         std::cout << "Error: mysql_init failed." << std::endl;
    14         return 0;
    15     }
    16     std::cout << "Info: mysql init successful." << std::endl;
    17 
    18     // 2、连接MYSQL 服务器
    19     MYSQL *ms_tmp_res = NULL;
    20     ms_tmp_res = mysql_real_connect(ms_conn, "localhost", "root", "123456sx", 
    21             "suyh", 0, NULL, 0);
    22     if (ms_tmp_res == NULL)
    23     {
    24         std::cout << "Error: connect mysql failed: " << mysql_error(ms_conn) << std::endl;
    25         mysql_close(ms_conn), ms_conn = NULL;
    26         return 0;
    27     }
    28     std::cout << "Info: mysql connect successful." << std::endl;
    29 
    30     // 3、执行SQL 语句
    31     std::string str_sqls = "";
    32     str_sqls += "SELECT id, account, sex, level, powers FROM player_data WHERE id = 100000 OR id = 100001 OR id = 100002";
    33 
    34     int res = 0;
    35     res = mysql_real_query(ms_conn, str_sqls.c_str(), str_sqls.size());
    36     if (res != 0)
    37     {
    38         std::cout << "Error: query failed, sql: " << str_sqls.c_str() << std::endl;
    39         std::cout << mysql_error(ms_conn) << std::endl;
    40     }
    41     else
    42     {
    43         std::cout << "Info: query successful." << std::endl;
    44 
    45         // 取出SQL 语句执行的结果集
    46         MYSQL_RES *ms_res = mysql_store_result(ms_conn);
    47         if (ms_res != NULL)
    48         {
    49             // 字段个数
    50             unsigned int field_num = mysql_num_fields(ms_res);
    51 
    52             // 每个字段的数据信息
    53             MYSQL_FIELD* field_info = mysql_fetch_field(ms_res);
    54             assert(field_info != NULL);
    55 
    56             // 每一个字段的值存储在row_data[i] 中,基本都以字符串的形式存储。NULL 的结果 指针也是NULL。
    57             // 二进制数据可以通过mysql_fetch_lengths() 函数得到该字段的二进制数据长度,同样以数组形式取出,即:lens[i];
    58             MYSQL_ROW row_data = NULL;    // mysql.h    ---    typedef char **MYSQL_ROW;
    59             while (1)
    60             {
    61                 row_data = mysql_fetch_row(ms_res);    // 取出下一行结果
    62                 if (row_data == NULL)
    63                     break;
    64 
    65                 std::cout << "##############################################" << std::endl;
    66                 for (int i = 0; i < field_num; ++i)
    67                 {
    68                     if (row_data[i] == NULL)
    69                         std::cout << "field name: '" << field_info[i].name << "', values is NULL." << std::endl;
    70                     else
    71                         std::cout << "field name: '" << field_info[i].name << "', values is " << row_data[i] << std::endl;
    72                 }
    73             }
    74         }
    75 
    76         // 释放结果集
    77         mysql_free_result(ms_res), ms_res = NULL;
    78     }
    79 
    80     // 使用完释放系统资源
    81     mysql_close(ms_conn), ms_conn = NULL;
    82     
    83     return 0;
    84 }
  • 相关阅读:
    商人的诀窍 结构体
    商人的诀窍 结构体
    小泉的难题 结构体
    小泉的难题 结构体
    来淄博旅游 结构体
    来淄博旅游 结构体
    分类游戏 结构体
    7月20日学习
    7月19日学习
    7月18日学习
  • 原文地址:https://www.cnblogs.com/suyunhong/p/4788949.html
Copyright © 2011-2022 走看看