用c在mysql数据库中插入变量

时间:2017-01-09 18:18:21

标签: mysql c

我正在尝试使用mysql API插入在我的C程序中计算的变量。尽管尝试了所有类型的技巧(单引号,双引号,查询占位符),但我无法输入数据。使用的库是:

 #include <stdio.h>
 #include <stdlib.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <math.h>
 #include <my_global.h>
 #include <mysql.h>

代码的相关位是

MYSQL *con = mysql_init(NULL);

if (mysql_real_connect(con, "localhost", "test", "test123", 
      "transport", 0, NULL, 0) == NULL) 
{
   finish_with_error(con);
}    
if (mysql_query(con, "INSERT INTO gps_points(vehicle_id,lat,lon,speed) VALUES(unit_id_dec,lat,lon,speed_float)")) 
{
   finish_with_error(con);
}
mysql_close(con);

我收到以下错误:

Unknown column 'unit_id_dec' in 'field list'

使用的编译选项是:

sudo gcc socketListen.c -o listenSock -lm `mysql_config --cflags --libs`

任何解决方案?

2 个答案:

答案 0 :(得分:0)

要解决此错误,您需要重写触发器并删除'unit_id_dec'变量或更改表并添加变量'unit_id_dec'。

Here's link how to rewrite trigger.

答案 1 :(得分:0)

我研究过并发现以下解决方案正在运行:

char buf[1024] = {};
char query_string[] = { 
   "INSERT INTO gps_points(vehicle_id,lat,lon,speed) VALUES(%u,%.4f,%.4f,%.2f)" 
                      };
sprintf(buf, query_string,unit_id_dec,lat,lon,speed_float);
if (mysql_query(con,buf)) 
{
  finish_with_error(con);
}