mysql - 如果我添加或减去一个值,我的变量中的值就会消失(不打印)

标签 mysql c data-conversion

我正在为学校做一个项目,我需要制作一个将温度保持在设定温度附近的系统(这是通过 phidget 传感器并转动加热和冷却来完成的) 元件打开和关闭。)传感器值可以使用公式((当前温度 + 50)* 4)转换为摄氏度,我让它在静态 20 摄氏度下工作,但现在我试图从一个数据库并将其保持在该温度附近(这样可以改变加热和冷却元件所在的房间内的温度)。但是当我尝试使用公式将从数据库获得的温度转换为传感器值时,临时变量开始给出随机数,我不知道为什么。有人可以解释或帮助吗?

(请参阅代码示例底部的注释以了解问题所在)

//my code is a code written for controlling the temprature in a room
//this is done by a phidget sensor and turning a heating and cooling
//element on and of.

#include <phidget21.h> // voor phidgetfucties
#include <stdio.h>   // voor printf()
#include <mysql/mysql.h> // voor mysql querys
#include <my_global.h>

int main (int argc, char* argv[])
{   //variables
int val;
int gewensteTempratuur = 280;
int gewensteGradenCelcius = 0;
int i = 0;
int temp = 0;

CPhidgetInterfaceKitHandle ifKit = 0;
CPhidgetInterfaceKit_create(&ifKit);

CPhidget_open((CPhidgetHandle)ifKit,-1);

for(;;)
{
    CPhidgetInterfaceKit_getSensorValue(ifKit,0,&val);


    printf("Value %d \n", val);

    if(val < (gewensteTempratuur - 4)){
    //verwarming
    CPhidgetInterfaceKit_setOutputState(ifKit,0,1);
    //airco
    CPhidgetInterfaceKit_setOutputState(ifKit,1,0);
    }

    if(val > (gewensteTempratuur + 4)){
        //verwarming
        CPhidgetInterfaceKit_setOutputState(ifKit,0,0);
        //airco
        CPhidgetInterfaceKit_setOutputState(ifKit,1,1);

    }

    if((val < (gewensteTempratuur + 4)) && (val > (gewensteTempratuur - 4)))         {
        //verwarming
        CPhidgetInterfaceKit_setOutputState(ifKit,0,0);
        //airco
        CPhidgetInterfaceKit_setOutputState(ifKit,1,0);

    }

    //Database Connection
    MYSQL *conn;
    //Verbings gegevens

    char *server = "server";
    char *user = "user";
    char *password = "pass";
    char *database = "database";

    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, server,
        user, password, database, 0, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    //getting information from the database
    if (mysql_query(conn, "SELECT Gewenstetemperatuur FROM SensorValue")){
        mysql_error(conn);
        }

        MYSQL_RES *result = mysql_store_result(conn);

        if (result == NULL){
        mysql_error(conn);
        }

        int num_fields = mysql_num_fields(result);

        MYSQL_ROW row;

        while ((row = mysql_fetch_row(result))){
            for (i = 0; i < num_fields; i++){
            printf("%s ", row[i] ? row[i] : "NULL");

            gewensteGradenCelcius = (int)row[i];
            temp = gewensteGradenCelcius;

            printf("\n %s \n", gewensteGradenCelcius);
            printf("%d \n",temp )

            //Here is were is get stuck if i try to take the value and run it trough a 
            //formula like (gewensteGradenCelcius + 50) * 4 and the try to print the 
            //gewensteGradenCelcius just puts out nothing
            }
            printf("\n");
            }

            mysql_free_result(result);
            usleep(1500000);
            }

    }

最佳答案

代码中包含 字符串 格式 (row[i]) 的数字转换不正确......

正如您所尝试的那样,

无法将 C string 复制到 int 中。 C 确实提供了多种方法来执行此操作,包括以下...更改:

gewensteGradenCelcius = (int)row[i];//incorrect

致:

//EITHER
gewensteGradenCelcius = atoi(row[i]);//simple and legal C99, but not recommended
//OR
char *endptr;
gewensteGradenCelcius = (int)strtol(row[i], &endptr, 10);//recommended method
//OR
sscanf(row[i], "%d",&gewensteGradenCelcius);//recommended method

(为什么不推荐atoi())

然后,因为 gewensteGradenCelciusint 您的 printf 语句,所以使用 int format specifier .变化:

printf("\n %s \n", gewensteGradenCelcius);//wrong format specifier for int

致:

printf("\n %d \n", gewensteGradenCelcius);//correct format specifier
           ^^

关于mysql - 如果我添加或减去一个值,我的变量中的值就会消失(不打印),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656653/

相关文章:

c# - 将 24 位二进制补码转换为 int?

Java - 如何测试一些代码?

php - Laravel hasOne 关系在 dd 上返回 null

mysql - 如何使用 Spring MVC 和 JPA 在 mysql 数据库中持久化图像

c - 到达终点所需的最小跳跃次数的线性时间算法

javascript - 从 webelement 读取字符串并将其转换为小数

Python SQLite3 : Cannot Create Table with Variable in Query

php - 在 Google GeoChart 中使用 PHP 处理从数据库中获取的数据

c - 为什么即使缺少 return 语句,C 中的程序仍能编译?

c - 对字符串的每个数字进行排序