0

I have prepared and executed a MYSQL_STMT and now I want to fetch the data.

I can get the number of fields with mysql_stmt_field_count.

I need to know the MYSQL_FIELD information for each field.

How do I get the MYSQL_FIELD information?

There is a field member of type MYSQL_FIELD* in MYSQL_STMT but in the manual it says:

The MYSQL_STMT structure has no members intended for application use.

There are several functions that take a MYSQL_RES such as mysql_fetch_fields, but I do not have a MYSQL_RES, I have a MYSQL_STMT

Documentation is here: http://dev.mysql.com/doc/refman/5.5/en/c.html

4

2 回答 2

0

如果您使用准备好的语句,您应该确切地知道每个字段的类型和大小,以便准备MYSQL_BIND数组

以下代码片段取自文档

MYSQL_BIND    bind[4]; // 4 columns
MYSQL_TIME    ts;
unsigned long length[4];
int           param_count, column_count, row_count;
short         small_data;
int           int_data;
char          str_data[STRING_SIZE];
my_bool       is_null[4];
my_bool       error[4];

/* Bind the result buffers for all 4 columns before fetching them */

memset(bind, 0, sizeof(bind));

/* INTEGER COLUMN */
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= (char *)&int_data;
bind[0].is_null= &is_null[0];
bind[0].length= &length[0];
bind[0].error= &error[0];

/* STRING COLUMN */
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (char *)str_data;
bind[1].buffer_length= STRING_SIZE;
bind[1].is_null= &is_null[1];
bind[1].length= &length[1];
bind[1].error= &error[1];

/* SMALLINT COLUMN */
bind[2].buffer_type= MYSQL_TYPE_SHORT;
bind[2].buffer= (char *)&small_data;
bind[2].is_null= &is_null[2];
bind[2].length= &length[2];
bind[2].error= &error[2];

/* TIMESTAMP COLUMN */
bind[3].buffer_type= MYSQL_TYPE_TIMESTAMP;
bind[3].buffer= (char *)&ts;
bind[3].is_null= &is_null[3];
bind[3].length= &length[3];
bind[3].error= &error[3];
于 2012-08-21T05:11:50.757 回答
-1

在将变量绑定到要用于获取列值的 MYSQL_BIND 结构之前,您可以检查结果集每一列的类型代码。如果您想确定最好使用哪些变量类型来避免类型转换,这可能是可取的。要获取类型代码,请在使用 mysql_stmt_execute() 执行准备好的语句后调用 mysql_stmt_result_metadata()。元数据提供对结果集的类型代码的访问,如第 22.9.7.23 节,“mysql_stmt_result_metadata()”和第 22.9.1 节,“C API 数据结构”中所述。

http://dev.mysql.com/doc/refman/5.5/en/mysql-stmt-result-metadata.html

于 2012-08-21T04:54:56.883 回答