2017-02-19 242 views
0

我想刪除使用libpq PQexecParams()函數的記錄。查詢成功返回,但所需行不會從表中刪除。這裏是我的代碼片段供參考。我已經使用PQexecParams()進行選擇併成功插入。你能幫忙嗎,我錯過了什麼!刪除記錄使用libpq PQexecParams()查詢

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); 

回答

0

我發現了這個問題。 我失蹤

meter_info_id = htonl(meter_info_id); 

通過添加它,解決了問題。