简体   繁体   English

使MySQL查询另存为全局变量是C

[英]Getting a MySQL Query to save as a global Variable is C

I am having issues getting a function to run and having the results of a MySQL query to be saved as a variable that other functions can use and call upon. 我在运行某个函数时遇到问题,并且将MySQL查询的结果另存为其他函数可以使用和调用的变量。 I know the results get read from the table as a string. 我知道结果是从表中以字符串形式读取的。 I was able to do this fine when getting the results and converting it to a float and then passing the results to a pointer. 当获得结果并将其转换为float并将结果传递给指针时,我能够做到这一点。 But I can not seem to figure out how to get the results as a string, and compare it with another string to see if they match or do not. 但是我似乎无法弄清楚如何以字符串形式获取结果,并将其与另一个字符串进行比较以查看它们是否匹配。 No matter what I have tried to do, I can not seem to get a value to be saved as a string to a variable outside the function. 无论我尝试执行什么操作,我似乎都无法获得将值另存为字符串的功能外部变量。

Here is the code of how I got it to work as a float: 这是我如何使其成为浮点数的代码:

(Outside the main function) (主要功能之外)

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

The function I have working with the float, that I can save to a global variable 我使用浮点运算的函数,可以将其保存到全局变量

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);
        }
    }
}

This is the function I can not get to work: 这是我无法使用的功能:

(Outside main Function) (主要功能外)

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

Function: 功能:

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 is where I want the string to be stored. char * ac是我想要存储字符串的地方。

You need to use strcpy in your MIA_get_desired_temperature function. 您需要在MIA_get_desired_temperature函数中使用strcpy。 Also, you don't need the pointer p_ac_mode. 另外,您不需要指针p_ac_mode。 Just copy into ac_mode directly. 只需直接复制到ac_mode即可。

strcpy(ac_mode, ac);

This line: 这行:

char *p_ac_mode = &ac_mode[256];

..is incorrect. ..是不正确的。 You're probably trying to declare a pointer to the array (or maybe to its contents)... what you're actually doing is declaring a char * that points at the first byte after the array ac_mode . 您可能正在尝试声明一个指向数组(或指向其内容)的指针...实际上,您在声明的char *指向数组ac_mode 之后的第一个字节。 The [256] here isn't indicating that ac_mode has 256 elements, it's indexing the array to get element 256 (which would be the 257th char in the array if it were big enough -- but it's not, so it's outside the array). 这里的[256]并不表示ac_mode具有256个元素,它正在对数组进行索引以获得256个元素(如果足够大,它将是数组中的第257个字符-但它不是,因此不在数组之外) 。 You're then taking the address of that out-of-bounds char and assign it to p_ac_mode , so that p_ac_mode to points to it. 然后,您将获取该边界char的地址,并将其分配给p_ac_mode ,以便p_ac_mode指向它。

To point p_ac_mode at the array contents, you'd just use char *p_ac_mode = ac_mode; 要将p_ac_mode指向数组内容,只需使用char *p_ac_mode = ac_mode; (which makes it a char * pointing at the first char in the array). (这使其成为一个char *指向数组中的第一个char )。 To get a pointer to the array itself, you'd use char (*p_ac_mode)[256] = &ac_mode; 要获得指向数组本身的指针,可以使用char (*p_ac_mode)[256] = &ac_mode; , which makes it a pointer to a 256-element array of char . ,这使其成为一个指向256个元素的char数组的指针。 In any case there's no need for p_ac_mode at all, because you can access the array through ac_mode directly in all the same places, and the bare array name ac_mode will usually decay to a pointer to its first char anyway. 无论如何,根本不需要p_ac_mode ,因为您可以在所有相同的地方直接通过ac_mode访问该数组,并且裸数组名称ac_mode通常将ac_mode衰减为指向其第一个char的指针。


With this line: 用这行:

                *p_ac_mode = *ac;

..you're copying the first char from string ac to the first char after ac_mode (because that's what p_ac_mode points to, as explained above). ..you're复制第一个char的字符串ac的第一个char ac_mode (因为这是p_ac_mode点,如上所述)。 I suspect you're actually trying to assign the whole ac string's contents to ac_mode via p_ac_mode -- but that won't work for a few reasons. 我怀疑你实际上想分配整个ac字符串的内容ac_mode通过p_ac_mode -但不会有以下几个原因工作。

An array is actually a block of memory holding a series of values of the same type. 数组实际上是存储一系列相同类型值的内存块。 Although in many situations the array name will decay to a pointer (the address of the array's first element), the array itself is the block of memory and not the pointer. 尽管在许多情况下,数组名称将衰减为指针(数组第一个元素的地址),但数组本身是内存块,而不是指针。 You can't just assign a pointer (new address) to the array, and array contents aren't automatically copied this way either. 您不能只为数组分配一个指针(新地址),并且数组内容也不会以这种方式自动复制。 The contents need to be copied over element by element, or using a function that copies the contents. 内容需要逐个元素复制,或使用复制内容的函数复制。

What you need to do is copy the contents of the string from your query results into the ac_mode array with strcpy() or similar. 您需要做的是使用strcpy()或类似方法将查询结果中的字符串内容复制到ac_mode数组中。 Just changing this line: 只需更改此行:

                *p_ac_mode = *ac;

to this: 对此:

                strcpy(ac_mode, ac);

...would do that. ...会那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM