MySQL C API句柄TEXT字段 [英] MySQL C API Handle TEXT field
问题描述
当使用MySQL C API中的Prepared Statements处理TEXT字段结果时,必须为out绑定指定字符串的长度:
When one uses Prepared Statements in MySQL C API to handle TEXT field result, one has to specify the length of the string for an out binding:
MYSQL_BIND out_bind;
char str_data[STRING_SIZE];
my_bool is_null;
my_bool error;
....
/* STRING COLUMN */
out_bind.buffer_type = MYSQL_TYPE_STRING;
out_bind.buffer = str_data;
out_bind.buffer_length = STRING_SIZE;
out_bind.is_null= &is_null;
out_bind.length= &length;
out_bind.error= &error;
mysql_stmt_bind_result(statement, out_bind)
在给定的示例中,STRING_SIZE是已知的常量,但是如何处理文本字段,其中数据长度可能从小到兆字节不等?
In the given example STRING_SIZE is the known constant, but how to be with TEXT fields where data length can vary from small sizes to megabytes?
有标准的方法吗?
推荐答案
mysql_stmt_fetch
的手册页说:
The manual page for mysql_stmt_fetch
says:
在某些情况下,您可能想确定一个列值的长度,然后再使用mysql_stmt_fetch()进行获取. ...为此,您可以使用以下策略:
In some cases you might want to determine the length of a column value before fetching it with mysql_stmt_fetch(). ... To accomplish this, you can use these strategies:
-
在调用
mysql_stmt_fetch()
检索单个行之前,将STMT_ATTR_UPDATE_MAX_LENGTH
传递给mysql_stmt_attr_set()
,然后调用mysql_stmt_store_result()
在客户端缓存整个结果.设置STMT_ATTR_UPDATE_MAX_LENGTH
属性会使列值的最大长度由mysql_stmt_result_metadata()
返回的结果集元数据的max_length
成员指示.
Before invoking
mysql_stmt_fetch()
to retrieve individual rows, passSTMT_ATTR_UPDATE_MAX_LENGTH
tomysql_stmt_attr_set()
, then invokemysql_stmt_store_result()
to buffer the entire result on the client side. Setting theSTMT_ATTR_UPDATE_MAX_LENGTH
attribute causes the maximal length of column values to be indicated by themax_length
member of the result set metadata returned bymysql_stmt_result_metadata()
.
为所涉及的列调用一个零长度缓冲区,并使用一个指针(可以存储实际长度)调用mysql_stmt_fetch()
.然后使用mysql_stmt_fetch_column()
的实际长度.
Invoke mysql_stmt_fetch()
with a zero-length buffer for the column in question and a pointer in which the real length can be stored. Then use the real length with mysql_stmt_fetch_column()
.
您可能还想阅读手册mysql_stmt_bind_result
You might also like to read the manual page for mysql_stmt_bind_result
这篇关于MySQL C API句柄TEXT字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!