enum enum_field_types {
MYSQL_TYPE_DECIMAL,
MYSQL_TYPE_TINY,
MYSQL_TYPE_SHORT,
MYSQL_TYPE_LONG,
MYSQL_TYPE_FLOAT,
MYSQL_TYPE_DOUBLE,
MYSQL_TYPE_NULL,
MYSQL_TYPE_TIMESTAMP,
MYSQL_TYPE_LONGLONG,
MYSQL_TYPE_INT24,
MYSQL_TYPE_DATE,
MYSQL_TYPE_TIME,
MYSQL_TYPE_DATETIME,
MYSQL_TYPE_YEAR,
MYSQL_TYPE_NEWDATE,
MYSQL_TYPE_VARCHAR,
MYSQL_TYPE_BIT,
MYSQL_TYPE_TIMESTAMP2,
MYSQL_TYPE_DATETIME2,
MYSQL_TYPE_TIME2,
MYSQL_TYPE_NEWDECIMAL=,
MYSQL_TYPE_ENUM=,
MYSQL_TYPE_SET=,
MYSQL_TYPE_TINY_BLOB=,
MYSQL_TYPE_MEDIUM_BLOB=,
MYSQL_TYPE_LONG_BLOB=,
MYSQL_TYPE_BLOB=,
MYSQL_TYPE_VAR_STRING=,
MYSQL_TYPE_STRING=,
MYSQL_TYPE_GEOMETRY=
} typedef struct st_mysql_field {
char *name; /* Name of column 列名 */
char *org_name; /* Original column name, if an alias */
char *table; /* Table of column if column was a field */
char *org_table; /* Org table name, if table was an alias */
char *db; /* Database for table */
char *catalog; /* Catalog for table */
char *def; /* Default value (set by mysql_list_fields) */
unsigned long length; /* Width of column (create length) */
unsigned long max_length; /* Max width for selected set */
unsigned int name_length;
unsigned int org_name_length;
unsigned int table_length;
unsigned int org_table_length;
unsigned int db_length;
unsigned int catalog_length;
unsigned int def_length;
unsigned int flags; /* Div flags */
unsigned int decimals; /* Number of decimals in field */
unsigned int charsetnr; /* Character set */
enum enum_field_types type; /* Type of field. See mysql_com.h for types */ // 字段的mysql 数据类型
void *extension;
} MYSQL_FIELD; int mysql_real_query(MYSQL *mysql, const char *q, unsigned long length);
说明:执行SQL
参数1:已初始化的MYSQL 实例;
参数2:SQL 语句;
参数3:SQL 语句字符数。 MYSQL_RES *mysql_store_result(MYSQL *mysql);
说明:对于成功检索了数据的每个查询(SELECT、SHOW、DESCRIBE、EXPLAIN、CHECK TABLE等),必须调用mysql_store_result()或mysql_use_result()
参数:已初始化的MYSQL 实例。 // typedef char **MYSQL_ROW;
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
说明:检索一个结果集合的下一行。当在mysql_store_result()之后使用时,如果没有更多的行可检索时,mysql_fetch_row()返回NULL。当在mysql_use_result()之后使用时,当没有更多的行可检索时或如果出现一个错误,mysql_fetch_row()返回NULL。
代码范例:
#include <iostream>
#include <mysql.h>
#include <string> #include <assert.h> int main()
{
// 1、初始化MYSQL 实例
MYSQL *ms_conn = mysql_init(NULL);
if (ms_conn == NULL)
{
std::cout << "Error: mysql_init failed." << std::endl;
return ;
}
std::cout << "Info: mysql init successful." << std::endl; // 2、连接MYSQL 服务器
MYSQL *ms_tmp_res = NULL;
ms_tmp_res = mysql_real_connect(ms_conn, "localhost", "root", "123456sx",
"suyh", , NULL, );
if (ms_tmp_res == NULL)
{
std::cout << "Error: connect mysql failed: " << mysql_error(ms_conn) << std::endl;
mysql_close(ms_conn), ms_conn = NULL;
return ;
}
std::cout << "Info: mysql connect successful." << std::endl; // 3、执行SQL 语句
std::string str_sqls = "";
str_sqls += "SELECT id, account, sex, level, powers FROM player_data WHERE id = 100000 OR id = 100001 OR id = 100002"; int res = ;
res = mysql_real_query(ms_conn, str_sqls.c_str(), str_sqls.size());
if (res != )
{
std::cout << "Error: query failed, sql: " << str_sqls.c_str() << std::endl;
std::cout << mysql_error(ms_conn) << std::endl;
}
else
{
std::cout << "Info: query successful." << std::endl; // 取出SQL 语句执行的结果集
MYSQL_RES *ms_res = mysql_store_result(ms_conn);
if (ms_res != NULL)
{
// 字段个数
unsigned int field_num = mysql_num_fields(ms_res); // 每个字段的数据信息
MYSQL_FIELD* field_info = mysql_fetch_field(ms_res);
assert(field_info != NULL); // 每一个字段的值存储在row_data[i] 中,基本都以字符串的形式存储。NULL 的结果 指针也是NULL。
// 二进制数据可以通过mysql_fetch_lengths() 函数得到该字段的二进制数据长度,同样以数组形式取出,即:lens[i];
MYSQL_ROW row_data = NULL; // mysql.h --- typedef char **MYSQL_ROW;
while ()
{
row_data = mysql_fetch_row(ms_res); // 取出下一行结果
if (row_data == NULL)
break; std::cout << "##############################################" << std::endl;
for (int i = ; i < field_num; ++i)
{
if (row_data[i] == NULL)
std::cout << "field name: '" << field_info[i].name << "', values is NULL." << std::endl;
else
std::cout << "field name: '" << field_info[i].name << "', values is " << row_data[i] << std::endl;
}
}
} // 释放结果集
mysql_free_result(ms_res), ms_res = NULL;
} // 使用完释放系统资源
mysql_close(ms_conn), ms_conn = NULL; return ;
}