我用 C 语言编写了一个用于读取传感器的代码。从那个传感器我得到一个 int 值。我将该 int 值存储在一个名为“val”的 int 变量中。
现在我想将存储在“val”中的值发送到 mysql 数据库中的表中。我只是在我的数据库中不断得到 0 。我不知道为什么。
这是代码:
int main (int argc, char* argv[])
{ int val; // val is a variable of type integer
CPhidgetInterfaceKitHandle ifKit =0;
CPhidgetInterfaceKit_create(&ifKit);
CPhidget_open((CPhidgetHandle)ifKit,-1);
CPhidget_waitForAttachment((CPhidgetHandle)ifKit,1000);
//Waiting max 1000ms till phidgetkit is connected.
while(1)
{
CPhidgetInterfaceKit_getSensorValue(ifKit,0,&val);
//Reading sensor 0 and stores value in variable val.
// &val = pointer to the memory location of val
if(val > 65){
CPhidgetInterfaceKit_setOutputState(ifKit, 0, 1);
}
else{
CPhidgetInterfaceKit_setOutputState(ifKit, 0, 0);
}
MYSQL *conn;
char *server = "*****";
char *user = "*****";
char *password = "*****";
char *database = "*****";
// Make the connection to the Mysql-database.
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
//Add values to table IRSensor
if (mysql_query(conn, "INSERT INTO IRsensor VALUES('val')" ))
{exit (1);}
printf("Value %d \n", val);
// Shows decimal value of val in terminal
usleep(800000); // delay of 800 ms before the sensor starts reading the next value
}
CPhidget_close((CPhidgetHandle)ifKit);
CPhidget_delete((CPhidgetHandle)ifKit);
return 0;
}