0

I am taking ID from my user and then i want to delete that ID from the database. I am using postgresql. but the query is not running. When i give hard code value like id=5 then it runs but when i give it user's value like id = DeleteId then it doesn't run. Here is the function:

void DeleteValue(PGconn *conn, int DeleteId)
      {

       PGresult *res = PQexec(conn, "DELETE FROM testData where ID =  DeleteId" );

         if (PQresultStatus(res) != PGRES_COMMAND_OK)
         {
           cout << "Delete testData record failed." << endl;
           PQclear(res);
           CloseConn(conn);
         } 
          cout << "\nDelete testData record - OK\n";
          PQclear(res);
      }

1 Answers1

0

Just solved my own problem.first concatenate it with the help of sprintf and then execute the query. Here is the solution of the problem

sprintf(sql,"DELETE FROM testData WHERE id = %d",DeleteId);
PGresult *res = PQexec(conn, sql );