[英]Putting a variable in sql query
嘿伙計們,我正在嘗試將一個變量放入 sql 查詢中,但似乎無法使其正常工作。 下面是我的代碼,它是一個 RFID 閱讀器,可以讀取 rfid 標簽的值。 我希望將 rfid 標簽的值存儲在數據庫中。 如何在 sql 查詢中獲取變量 TagVal? TagVal 變量需要位於 1e00ad0ca9 值所在的位置(下面的第二個查詢)。 謝謝!
#include <stdio.h>
#include <phidget21.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int CCONV TagHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto) //Tag wordt gelezen en LED gaat aan
{
CPhidgetRFID_setLEDOn(RFID, 1);
printf("Tag Read: %s\n", TagVal);
return 0;
}
int CCONV TagLostHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto) //Tag is verwijderd van RFID lezer
{
CPhidgetRFID_setLEDOn(RFID, 0);
printf("Tag Lost: %s\n", TagVal);
return 0;
}
int rfid_simple()
{
int result;
const char *err;
//Declare an RFID handle
CPhidgetRFIDHandle rfid = 0;
//RFID object wordt aangemaakt
CPhidgetRFID_create(&rfid);
//Wanneer de tag gelezen wordt, wordt het uitgeprint
CPhidgetRFID_set_OnTag2_Handler(rfid, TagHandler, NULL);
//Wanneer de tag verwijderd wordt, wordt het uitgeprint
CPhidgetRFID_set_OnTagLost2_Handler(rfid, TagLostHandler, NULL);
//RFID wordt geopend voor connectie
CPhidget_open((CPhidgetHandle)rfid, -1);
//Programma wacht totdat RFID is aangesloten
printf("Waiting for RFID to be attached....\n");
if((result = CPhidget_waitForAttachment((CPhidgetHandle)rfid, 10000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 0;
}
CPhidgetRFID_setAntennaOn(rfid, 1);
//read RFID event data
printf("RFID reader is aangesloten! Druk op enter om het programma af te sluiten\n");
//keep displaying RFID event data until user input is read
printf("RFID tag nodig\n");
getchar();
//RFID wordt afgesloten zodra er op Enter wordt gedrukt
printf("Ik ga afsluiten vriend...\n");
CPhidget_close((CPhidgetHandle)rfid);
CPhidget_delete((CPhidgetHandle)rfid);
return 0;
}
int main(int argc, char* argv[])
{
MYSQL *conn;
char *server = "x";
char *user = "x";
char *password = "x";
char *database = "x";
// Make the connection to the Mysql-database on server.
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);
}
//een insert into query : insert into tabelnaam VALUES(kolomnaam1, kolomnaam2, etc... )
if (mysql_query(conn, "INSERT INTO Notifications(Notification_id, Notification) VALUES(NULL,'Open Deur')" )!=0)
{exit(1);}
if (mysql_query(conn, "INSERT INTO Access(Employee_id, Notification_id)VALUES('1e00ad0ca9',1)" )!=0)
{exit(1);}
/* close connection */
mysql_close(conn);
rfid_simple();
return 0;
}
您可以使用sprintf來構建您的字符串並輕松連接您的變量 - 替代 malloc/free。
char buffer [1024];
char *tagVal1 = "hello";
char *tagVal2 = "world!";
n=sprintf (buffer, "INSERT INTO Notifications(Notification_id, Notification) VALUES(%s,%s)", tagVal1, tagVal2);
printf ("[%s] is a string %d chars long\n",buffer,n);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.