2017-01-09 82 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