当使用MySQL C API中的Prepared Statements来处理TEXT字段结果时,必须指定out绑定的字符串长度:
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是已知常量,但如何使用TEXT字段,其中数据长度可以从小尺寸变为兆字节?
这有标准的方法吗?
解决方法:
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:
Before invoking mysql_stmt_fetch() to retrieve individual rows, pass STMT_ATTR_UPDATE_MAX_LENGTH to mysql_stmt_attr_set(), then invoke mysql_stmt_store_result() to buffer the entire result on the client side. Setting the STMT_ATTR_UPDATE_MAX_LENGTH attribute causes the maximal length of column values to be indicated by the max_length member of the result set metadata returned by mysql_stmt_result_metadata().
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,c-3,mysql-connector
来源: https://codeday.me/bug/20190626/1295215.html