2017-01-09 83 views
0

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

#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` 

任何解决方案?

+0

看看http://dev.mysql.com/doc/refman/5.7/en/c- api-prepared-statement-function-overview.html – dbush

回答

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

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

Here's link how to rewrite trigger.

+0

unit_id_dec是需要插入值的变量。这不是专栏 – rohit