Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将MySQL查询结果保存到c中的变量中_Mysql_C - Fatal编程技术网

如何将MySQL查询结果保存到c中的变量中

如何将MySQL查询结果保存到c中的变量中,mysql,c,Mysql,C,我有下面的MySQL查询,当在MySQL控制台中完成时,它会显示正确的结果。然而,我不知道如何将结果存储到我的c程序中的变量中 以下是我在MySQL界面中键入的代码: mysql> SELECT id FROM Stations where name = 'AE0'; 这就是它的回报: +----+ | id | +----+ | 1 | +----+ 我需要将上述值“1”存储到我的c程序中的一个变量中。使用以下代码从我的c程序调用MySQL查询: MYSQL_RES *result

我有下面的MySQL查询,当在MySQL控制台中完成时,它会显示正确的结果。然而,我不知道如何将结果存储到我的c程序中的变量中

以下是我在MySQL界面中键入的代码:

mysql> SELECT id FROM Stations where name = 'AE0';
这就是它的回报:

+----+
| id |
+----+
|  1 |
+----+
我需要将上述值“1”存储到我的c程序中的一个变量中。使用以下代码从我的c程序调用MySQL查询:

MYSQL_RES *result;
MYSQL_ROW row;
length=sprintf(query,"SELECT id FROM Stations where name ='AE0'");
myquery(conn,query,length);
result=mysql_store_result(conn);
row=mysql_fetch_row(result);
我不确定我要查找的值是否存储在“结果”中,无论是不是,我如何找到它并将其保存为整数?

简短回答:

int i = atoi(row[0]);
长答覆:

返回一个MYSQL\u行。然后必须遍历该行以获得每个值。这些值是字符串,因此需要使用
atoi()
将它们转换为int。下面的代码(除了
atoi()
调用)从上面的链接无耻地窃取了这一点:

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");
}
MYSQL\u行;
无符号整数字段;
无符号整数i;
num\u fields=mysql\u num\u fields(结果);
while((row=mysql\u fetch\u row(result)))
{
无符号长*长度;
长度=mysql_fetch_长度(结果);
对于(i=0;i
简短回答:

int i = atoi(row[0]);
长答覆:

返回一个MYSQL\u行。然后必须遍历该行以获得每个值。这些值是字符串,因此需要使用
atoi()
将它们转换为int。下面的代码(除了
atoi()
调用)从上面的链接无耻地窃取了这一点:

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");
}
MYSQL\u行;
无符号整数字段;
无符号整数i;
num\u fields=mysql\u num\u fields(结果);
while((row=mysql\u fetch\u row(result)))
{
无符号长*长度;
长度=mysql_fetch_长度(结果);
对于(i=0;i