gpt4 book ai didi

MySQL C API 处理文本字段

转载 作者:可可西里 更新时间:2023-11-01 07:33:34 34 4
gpt4 key购买 nike

当使用 MySQL C API 中的 Prepared Statements 处理 TEXT 字段结果时,必须为 out 绑定(bind)指定字符串的长度:

 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 字段?

有这方面的标准方法吗?

最佳答案

manual page for 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:

  • 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().

您可能还想阅读 manual page for mysql_stmt_bind_result

关于MySQL C API 处理文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6615204/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com