0

I am trying to delete a record using libpq PQexecParams() function. The query is successfully returned but required row is not deleted from the table. Here is the snippet from my code for reference. I have used PQexecParams() for select and insert successfully. Could you please help, what am I missing!

   PGresult   *res;
   int meter_info_id;

    printf ("Enter Meter Information Id");
    scanf("%d", &meter_info_id);

     char *stm_write_reg = "delete from write_reg_set where  meter_id=$1";

     int nparam = 1;

     //set the values to use
     const char *values[1] = {(char *)&meter_info_id};

     //calculate the lengths of each of the values
     int lengths[1] = {sizeof(meter_info_id)};

     //state which parameters are binary
     int binary[1] = {1};


     res = PQexecParams(conn,
             stm_write_reg,
             nparam,  //number of parameters
             NULL,    //ignore the Oid field
             values,  //values to substitute $1 and $2 and so on
             lengths, //the lengths, in bytes, of each of the parameter values
             binary,  //whether the values are binary or not
             0);      //we want the result in text format

     if (PQresultStatus(res) != PGRES_COMMAND_OK)
     {
         fprintf(stderr, "stm_write_reg failed: %s", PQerrorMessage(conn));
         exit_nicely(conn,res);
     }


     PQclear(res);

1 Answers1

0

I have found the problem. I was missing

meter_info_id = htonl(meter_info_id);

By adding it, fixed the problem.