在sql查询中放置一个变量

时间:2015-11-27 19:51:02

标签: mysql sql c

嘿伙计们我试图在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;
 }

1 个答案:

答案 0 :(得分:1)

您可以使用 sprintf 来构建字符串并轻松连接变量 - 替代malloc / free。

CREATE TABLE catDelegacion (
  id_delegacion serial NOT NULL,
  delegacion character varying,
  CONSTRAINT catDelegacion_pkey PRIMARY KEY (id_delegacion)
);

INSERT INTO catDelegacion(delegacion)
  VALUES ('someData');