2014-04-07 52 views
0

以下程序嘗試將新行和新列插入到已經存在的數據庫中,其中包含四個列(id,lastname,firstname,phonenumber)的表「people」。當該行成功插入時,該列不會被添加。無法將列添加到使用C的現有表中

#include <stdio.h> 
    #include <stdlib.h> 
    #include <libpq-fe.h> 
    #include <string.h> 

    int main() 
    { 
    PGconn *conn; 
    PGresult *res; 
    int rec_count; 
    int row; 
    int col; 

    conn = PQconnectdb("dbname=test host=localhost user=abc1 password=xyz1"); 

     if(PQstatus(conn) == CONNECTION_BAD) { 
      puts("We were unable to connect to the database"); 
      exit(0); 
     } 

    res = PQexec(conn,"INSERT INTO people VALUES (5, 'XXX', 'YYY', '7633839276');"); 
    if(PQresultStatus(res) != PGRES_COMMAND_OK) { 
     fprintf(stderr, "Insertion Failed1: %s", PQerrorMessage(conn)); 
     PQclear(res); 
     } 
    else 
     printf("Successfully inserted value in Table..... \n"); 

     res = PQexec(conn,"update people set phonenumber=\'5055559999\' where id=3"); 
    if(PQresultStatus(res) != PGRES_COMMAND_OK) { 
     fprintf(stderr, "Insertion Failed2: %s", PQerrorMessage(conn)); 
     PQclear(res); 
     } 

    res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'"); 
    if(PQresultStatus(res) != PGRES_COMMAND_OK) { 
     fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn)); 
     PQclear(res); 
     } 

    rec_count = PQntuples(res); 

     printf("We received %d records.\n", rec_count); 
     puts("=========================="); 

     for(row=0; row<rec_count; row++) { 
      for(col=0; col<3; col++) { 
       printf("%s\t", PQgetvalue(res, row, col)); 
      } 
      puts(""); 
     } 

     puts("=========================="); 

     PQclear(res); 

     PQfinish(conn); 

     return 0; 
    } 

程序的編譯和鏈接後,給出了以下的輸出:

$ ./test 
    Successfully inserted value in Table..... 
    We received 0 records. 
    ========================== 
    ========================== 

PostgreSQL的環境,表「人」是一個額外的行包含列「TRUE」更新。

這是我用C嵌入postgresql的第一個程序。請幫忙!!

+0

編輯丹尼爾· – user3258515

+0

的建議發佈它增加了行和山坳,但只有在PostgreSQL環境而不是內部的工作代碼該程序。 – user3258515

回答

0

這條線:

res = PQexec(conn, "EXEC('UPDATE people SET comment = ''TRUE'';');"); 

應該是:

res = PQexec(conn, "UPDATE people SET comment = 'TRUE'"); 

EXEC語法在C與ECPG嵌入式SQL的一部分。它不能與C libpq library中的PQexec結合使用,這顯然是您在給定源代碼的其餘部分時使用的。

還沒有RETURNING子句的UPDATE的結果具有PQresultStatus(res)==PGRES_COMMAND_OK,不PGRES_TUPLES_OK

PGRES_COMMAND_OK

Successful completion of a command returning no data. 

Command Execution Functions

您也想測試通過返回的任何PGresultPQexec,不僅是您最後一次查詢的結果,因爲任何查詢都可能失敗。

+0

在不知道確切用法的情況下對EXEC進行道歉。我最初按照你的建議做了替代,但沒有奏效。谷歌搜索告訴我這個EXEC功能,我想我的運氣。但是,沒有任何工作仍然!該行總是被添加,但不是列「評論」。我也相應地更改了PQresultStatus(res)行。 – user3258515

+0

@ user3258515:請參閱您對INSERT的錯誤檢查和顯示類型,您需要對ALTER TABLE語句使用相同的方法。事實上,每次調用PQexec。 –

+0

錯誤檢查每個PQexec步驟顯示用戶沒有權限,因此在糾正後,我可以插入列,但只能在postgresql環境中。該程序,但是,並沒有給出輸出。它給出以下輸出:成功插入表中的值..... 我們收到0條記錄。 我們沒有得到任何數據!這意味着rec_count = PQntuples(res);不管用。你能提出一個可能的錯誤嗎? – user3258515

0

如何在不執行SELECT語句的情況下檢索數據?

從未使用過的PQexec,但我想添加的代碼可能是這樣的:

res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'"); 
if(PQresultStatus(res) != PGRES_COMMAND_OK) { 
    fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn)); 
    PQclear(res); 
} 

//Add PQexec and select statement here. 
//Something "like" this. 
res = PQexec(conn, "SELECT * FROM people;"); 
if(PQresultStatus(res) != PGRES_TUPLES_OK) { 
    fprintf(stderr, "Select Failed: %s", PQerrorMessage(conn)); 
    PQclear(res); 
} 

    rec_count = PQntuples(res); 

    printf("We received %d records.\n", rec_count); 
+0

我希望你昨天回覆...我一直在努力,最終以正確的方式解決了上述問題。所以,感謝你和丹尼爾,他們一直在推動我..希望有投票選項兩個答案! – user3258515

相關問題