mysql - C - 获取 MySQL 行到字符

标签 mysql c

我想从我的数据库 (MySQL) 中获取结果并将它们存储在一个数组中。我尝试了一些事情,但我没有解决办法。

    mysql_real_query(mysql, query, strlen(query));
    mysql_res = mysql_store_result(mysql);
    rows_number = (unsigned long) mysql_num_rows (mysql_res);
    printf ("Number of Rows: %lu\n\n", rows_number);

    while ((row = mysql_fetch_row (mysql_res)) != NULL) {
    for (i = 0;  i < mysql_num_fields(mysql_res);  i ++)
    printf ("%s ",row[i]); //this is printed like it should be.
    printf("\n");

    char *val = row[i]; //should save it to an char
    printf("%s",val);

但是如果我启动程序,输出是:(仅提取!) 这是 printf ("%s ",row[i]); 发送的内容:

1 1 1 0006 

这是打印出来的 char val:

</alias>
<alias>windows-1256</alias>
<collation name="cp1256_bin"        id="67" order="Binary"  flag="binary"/>
<collation name="cp1256_general_ci" id="57" order="Arabic"  flag="primary">
<order>Arabic</order>
<order>Persian</order>
<order>Pakistani</order>
<order>Urdu</order>
</collation>
</charset>

那么有什么问题呢? 感谢您的帮助!!

最佳答案

i 正在 for 循环的边界之外使用:

代替:

 for (i = 0;  i < mysql_num_fields(mysql_res);  i ++)
     printf ("%s ",row[i]); //this is printed like it should be.

你可能是这个意思:

 for (i = 0;  i < mysql_num_fields(mysql_res);  i ++)
 {
    printf ("%s ",row[i]); //this is printed like it should be.
    printf("\n");

    char *val = row[i]; //should save it to an char
    printf("%s",val);
 }

关于mysql - C - 获取 MySQL 行到字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22477824/

相关文章:

php - 按平台选择和统计不同的活跃用户,并比较它们是否存在于其他表中

mysql - 试图获取 sqlite db 中 3 列的 DISTINCT 值

c - 矩阵乘法 - C

c++ - 指针可以指向4GB之后的地址吗?

c - 如何多次使用 strchr() 查找第 n 次出现

C#存储球员卡数据库设计

MySQL GROUP_CONCAT 分配增量值

php - 如何使用 2 个 WHERE 子句(包括两个表,其中查询中的一列)

c - 文件处理和导出到文本文件

c - 如何在 Perl 中有效解析表达式 '^#[0-9A-F]{2}\$[0-9A-F]{4}/$'