当我尝试使用代码时,我发现row [i]是字符串形式的第i行。但是我需要使用int形式的数据。另外,row [i]包含字符串形式的整个行。如何从中提取数据。我试图解析数据并将其转换为整数,但是整个数据只是一个字符串,并且没有任何空格!所以我很难解析,因为我无法知道数据集中上一个字段在哪里结束而下一个字段在哪里开始。

总结一下;

当我做:

mysql_real_connect(conn, "localhost", "root", "abcd", "Hybr", 0, NULL, 0);
    mysql_query(conn, "SELECT * FROM Data");
    result = mysql_store_result(conn);

    num_fields = mysql_num_fields(result);

    while ((row = mysql_fetch_row(result)))
    {
            for(i = 0; i < num_fields; i++)
            {
                  row[i]  //this is a string containing all the
                                  //fields. I want the individual values !

            }
  }


我的数据34、45主机被转换为字符串“ 34 45 host”。而且更奇怪的是,当我打印row [i]时,它至少会打印空格,但是当我将其复制到char *中时,空格会以某种方式消失!因此就无法解析它。

我认为有一种不同的读取记录的方法,也许我已经忽略了API的某些部分,但是我似乎找不到哪个...

编辑

我意识到我没有忽略API;只是row [i]是一个字符串数组。我仍然需要帮助来从中提取单个值。

最佳答案

如果您知道它将是int,则可以在字段上使用atoi。例如,单位是int,因此“从数据中选择单位”可以读入int数组或其他内容。

mysql_query  conn, "SELECT units FROM data"
res = mysql_store_result(conn)
num_fields = mysql_num_fields(res)
while  (row = mysql_fetch_row(res))
    for  i = 0; i < num_fields; i++
        printf  "%03i ",atoi(row[i])
    puts  ""

关于mysql - 读取由row = mysql_fetch_row(result)返回的mySQL数据库的行集,这是一个字符串数组,用于从中提取单个字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9751112/

10-11 05:46
查看更多