gpt4 book ai didi

mysql - 获取 MySQL 查询以保存为全局变量是 C

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:21 25 4
gpt4 key购买 nike

我在运行一个函数以及将 MySQL 查询的结果保存为其他函数可以使用和调用的变量时遇到问题。我知道结果是作为字符串从表中读取的。在获取结果并将其转换为 float 然后将结果传递给指针时,我能够做到这一点。但我似乎无法弄清楚如何将结果作为字符串获取,并将其与另一个字符串进行比较以查看它们是否匹配。无论我尝试做什么,我似乎都无法将值作为字符串保存到函数外部的变量中。

这是我如何让它作为 float 工作的代码:

(在主函数之外)

float temperature_reading;
float *p_temperature_reading= &temperature_reading;
float humidity_reading;
float *p_humidity_reading= &humidity_reading;

我使用 float 的函数,我可以将其保存到全局变量

void MIA_get_current_temperature()
{

const char *query = "SELECT Temperature, Humidity FROM `temperature` WHERE Type='Current_Temperature'";

if (mysql_query(conn, query) != 0)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(-1);
} else {
MYSQL_RES *query_results = mysql_store_result(conn);
if (query_results)
{ // make sure there *are* results..
MYSQL_ROW row;

while((row = mysql_fetch_row(query_results)) !=0)
{

float f = row[0] ? atof(row[0]) : 0.0f;
float h = row[1] ? atof(row[1]) : 0.0f;

*p_temperature_reading = f;
*p_humidity_reading = h;

printf("The Temp & Hum from DB is: %.1f & %.1f\n", *p_temperature_reading,*p_humidity_reading);
}

/* Free results when done */
mysql_free_result(query_results);
}
}
}

这是我无法使用的功能:

(在主函数之外)

 char ac_mode[256];
char *p_ac_mode = &ac_mode[256];

功能:

void MIA_get_desired_temperature()
{

const char *query = "SELECT Mode, Desired_Temperature, Threshold FROM `ac_mode` WHERE Status='ON'";

if (mysql_query(conn, query) != 0)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(-1);
} else {
MYSQL_RES *query_results = mysql_store_result(conn);
if (query_results)
{ // make sure there *are* results..
MYSQL_ROW row;

while((row = mysql_fetch_row(query_results)) !=0)
{

char *ac = row[0] ? row[0] : "NULL";
float desired_temperature = row[1] ? atof(row[1]) : 0.0f;
int threshold = row[2] ? atof(row[2]) : 0.0f;

*p_ac_mode = *ac;
*p_desired_temperature = desired_temperature;
*p_threshold=threshold;

}

/* Free results when done */
mysql_free_result(query_results);
}
}
}

char *ac 是我想要存储字符串的地方。

最佳答案

这一行:

char *p_ac_mode = &ac_mode[256];

..不正确。您可能正在尝试声明一个指向数组(或者可能指向其内容)的指针......您实际上正在做的是声明一个 char *指向数组ac_mode 之后 的第一个字节. [256]这里并不表示 ac_mode有 256 个元素,它索引数组以获取元素 256(如果它足够大,它将是数组中的第 257 个字符——但事实并非如此,所以它在数组之外)。然后,您将获取该越界地址 char并将其分配给 p_ac_mode ,所以 p_ac_mode指向它。

指向p_ac_mode在数组内容中,您只需使用 char *p_ac_mode = ac_mode; (这使得它成为一个 char * 指向数组中的第一个 char)。要获得指向数组本身的指针,您可以使用 char (*p_ac_mode)[256] = &ac_mode; , 这使它成为指向 char 的 256 元素数组的指针.无论如何都不需要 p_ac_mode完全可以,因为您可以通过 ac_mode 访问数组直接在所有相同的地方,裸数组名称 ac_mode通常会衰减到指向其第一个 char 的指针无论如何。


用这一行:

                *p_ac_mode = *ac;

..你正在复制第一个 char来自字符串 ac到第一个char 之后 ac_mode (因为这就是 p_ac_mode 指向的内容,如上所述)。我怀疑您实际上是在尝试分配整个 ac字符串的内容为ac_mode通过p_ac_mode -- 但由于一些原因,这不会奏效。

数组实际上是一 block 内存,其中包含一系列相同类型的值。尽管在许多情况下数组名称会衰减为指针(数组第一个元素的地址),但数组本身是内存块而不是指针。您不能只为数组分配一个指针(新地址),并且数组内容也不会以这种方式自动复制。需要逐个元素地复制内容,或者使用复制内容的函数。

您需要做的是将查询结果中的字符串内容复制到ac_mode 中。数组 strcpy()或类似的。只需更改此行:

                *p_ac_mode = *ac;

为此:

                strcpy(ac_mode, ac);

...会那样做。

关于mysql - 获取 MySQL 查询以保存为全局变量是 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35074059/

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