MYSQL_ROW 和 row[0] 转换为字符串类型?

发布于 2024-07-08 18:50:05 字数 157 浏览 6 评论 0原文

使用MYSQL C API查询结果时。 结果以 MYSQL_ROW 类型返回,根据 MYSQL C API 文档,我可以轻松 printf("%s", row[0])。 但是如果我想将 row[0] 的内容传输到字符串或 char* 中怎么办?

When using MYSQL C API to query results. The results are returned as a MYSQL_ROW type, which according to the MYSQL C API documentation, I can easily printf("%s", row[0]). But what if I want to transfer the contents of row[0] into a string or a char*?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短叹 2024-07-15 18:50:05

%s 格式应该只接受 char* 因此从你的描述看来 MYSQL_ROW 实际上是一个 char** 并且无论如何采用 row[0] 都会产生一个 char* 。

我不明白使用 sprintf() (或更安全但非标准的 asprintf())有什么好处,但如果它让您感觉更好,您可以考虑它。

The %s format is only supposed to accept char* so from your description it looks like MYSQL_ROW is really a char** and taking row[0] will yield a char* anyway.

I don't see how using sprintf() (or the safer but non standard asprintf()) would be of any benefit but you may consider it if it makes you feel better.

紫瑟鸿黎 2024-07-15 18:50:05

您可以使用 sprintf(),但您仍然需要知道字符串包含在 row[0] 中,以便您可以分配足够的内存。

警告:BCS 正确指出,如果 row[0] 包含额外的 nul 字节,您的数据将被截断。 如果您知道 row[0] 中存储了多少数据,请使用 memcpy() 可能是一个更好的解决方案。

You could use sprintf(), but you will still need to know the length of the string contained in row[0] so you can allocate enough memory.

Caveat: BCS correctly points out that your data will be truncated if row[0] contains extra nul bytes. If you know how much data is stored in row[0], using memcpy() is probably a better solution.

む无字情书 2024-07-15 18:50:05

但是如果我想将 row[0] 的内容传输到字符串或 char* 中怎么办?

使用mysql_fetch_lengths确定MYSQL_ROW中数组的长度。 因为行中有 1 个或多个列,所以可以有 1 个或多个长度。

根据 27.8.5 C API 数据结构< /a> 文档:

MYSQL_ROW

这是一行数据的类型安全表示。 目前是
实现为计数字节字符串的数组。 (你不能治疗
如果字段值可能包含二进制,则这些为空终止字符串
数据,因为这些值可能在内部包含空字节。)行是
通过调用mysql_fetch_row()获取。

然后, mysql_fetch_row< /a> 文档给出了这个例子:

MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;

num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
   unsigned long *lengths;
   lengths = mysql_fetch_lengths(result);
   for(i = 0; i < num_fields; i++)
   {
       printf("[%.*s] ", (int) lengths[i],
              row[i] ? row[i] : "NULL");
   }
   printf("\n");
}

这是一个使用它来计算表中行数的示例。 该表是根据 FTC 的 Do 填充的没有通话违规

MYSQL_RES* res = NULL;
MYSQL row;

const char SELECT_STMT[] = "SELECT COUNT(*) FROM blacklist_ftc";
if (mysql_query(mysql, SELECT_STMT) != 0)
{
    /* handle error */
    goto finish;
}

if ((res = mysql_store_result(s_mysql)) == NULL)
{
    /* handle error */
    goto finish;
}

if (mysql_num_fields(res) != 1 || mysql_num_rows(res) != 1)
{
    /* handle error */
    goto finish;
}

char buf[32];
unsigned long* length;

row = mysql_fetch_row(res);
length = mysql_fetch_lengths(res);

if (row == NULL || length == NULL)
{
    /* handle error */
    goto finish;
}

size_t size = length[0] < sizeof(buf)-1 ? length[0] : sizeof(buf)-1;
memcpy(buf, row[0], size);
buf[size] = '\0';

unsigned long count = (unsigned long)atoi(buf);

finish:

if (res)
{
    mysql_free_result(res);
    res = NULL;
}

But what if I want to transfer the contents of row[0] into a string or a char*?

Use mysql_fetch_lengths to determine the length of the array in the MYSQL_ROW. Because there is 1 or more columns in the row, there can be 1 or more lengths.

According to 27.8.5 C API Data Structures documentation:

MYSQL_ROW

This is a type-safe representation of one row of data. It is currently
implemented as an array of counted byte strings. (You cannot treat
these as null-terminated strings if field values may contain binary
data, because such values may contain null bytes internally.) Rows are
obtained by calling mysql_fetch_row().

Then, the mysql_fetch_row documentation gives this example:

MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;

num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
   unsigned long *lengths;
   lengths = mysql_fetch_lengths(result);
   for(i = 0; i < num_fields; i++)
   {
       printf("[%.*s] ", (int) lengths[i],
              row[i] ? row[i] : "NULL");
   }
   printf("\n");
}

Here is an example of using it to count rows in a table. The table was populated from the FTC's Do No Call violations.

MYSQL_RES* res = NULL;
MYSQL row;

const char SELECT_STMT[] = "SELECT COUNT(*) FROM blacklist_ftc";
if (mysql_query(mysql, SELECT_STMT) != 0)
{
    /* handle error */
    goto finish;
}

if ((res = mysql_store_result(s_mysql)) == NULL)
{
    /* handle error */
    goto finish;
}

if (mysql_num_fields(res) != 1 || mysql_num_rows(res) != 1)
{
    /* handle error */
    goto finish;
}

char buf[32];
unsigned long* length;

row = mysql_fetch_row(res);
length = mysql_fetch_lengths(res);

if (row == NULL || length == NULL)
{
    /* handle error */
    goto finish;
}

size_t size = length[0] < sizeof(buf)-1 ? length[0] : sizeof(buf)-1;
memcpy(buf, row[0], size);
buf[size] = '\0';

unsigned long count = (unsigned long)atoi(buf);

finish:

if (res)
{
    mysql_free_result(res);
    res = NULL;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文