2017-05-11 28 views
1

我試圖將多行傳遞給測試表,並且我無法理解它在libpq中的完成方式。libpq如何傳遞批量數據

我發現了用於複製我認爲需要的數據的命令,但沒有任何有關如何使用它們的示例。 https://www.postgresql.org/docs/8.3/static/libpq-copy.html

這是我想到的代碼,但我在PQputCopyEnd函數中得到了段錯誤。我在這裏很迷路,所以任何幫助都會很棒。

/* 
* testlibpq.c 
* 
*  Test the C version of libpq, the PostgreSQL frontend library. 
*/ 
#include <stdio.h> 
#include <string> 
#include <stdlib.h> 
#include <postgresql/libpq-fe.h> 

static void exit_nicely(PGconn *conn) 
{ 
    PQfinish(conn); 
    exit(1); 
} 

int main(int argc, char **argv) 
{ 
    const char *conninfo, *errmsg; 
    PGconn  *conn; 
    PGresult *res; 

    //std::string buffer = "key1\tcol11\tcol12\nley2\tcol21\tcol22"; 
    std::string buffer = "key1\tcol11\tcol12"; 


    if (argc > 1) 
     conninfo = argv[1]; 
    else 
     conninfo = "dbname=postgres host=129.24.26.136 user=postgres password=postgresUNM"; 

    /* Make a connection to the database */ 
    conn = PQconnectdb(conninfo); 

    /* Check to see that the backend connection was successfully made */ 
    if (PQstatus(conn) != CONNECTION_OK) 
    { 
     fprintf(stderr, "Connection to database failed: %s", 
       PQerrorMessage(conn)); 
     exit_nicely(conn); 
    } 

    //do stuff here 
    res = PQexec(conn, "COPY cplusplustest from STDIN"); 
    int a = PQputCopyData(conn, buffer.c_str(), buffer.length()); 

    res = PQexec(conn, "COMMIT"); 

    int b = PQputCopyEnd(conn, errmsg); 

    if (errmsg == NULL) 
    { 
     printf("worked.\n"); 
    } 

    /* close the connection to the database and cleanup */ 
    PQfinish(conn); 

    return 0; 
} 

回答

0
  • Postgres的8.3已經很老了;請考慮一個較新版本
  • 的ERRMSG參數b = PQputCopyEnd(conn, errmsg);必須設置爲NULL(這是輸入的libpq,表明客戶已中止複印件)(the manual仍然是相當模糊的,我同意。
  • 我刪除了C++。
  • 提交的CopyEnd後應該去

/* 
* testlibpq.c 
* 
*  Test the C version of libpq, the PostgreSQL frontend library. 
*/ 
#include <stdio.h> 
#include <string.h>  // <<-- 
#include <stdlib.h> 

// #include <postgresql/libpq-fe.h> // <<-- 
#include <libpq-fe.h> 

static void exit_nicely(PGconn *conn) 
{ 
    PQfinish(conn); 
    exit(1); 
} 
int main(int argc, char **argv) 
{ 
    const char *conninfo, *errmsg; 
    PGconn  *conn; 
    PGresult *res; 
    int a,b; // <<-- 

    char buffer[] = "key1\tcol11\tcol12"; 

    if (argc > 1) 
     conninfo = argv[1]; 
    else 
     conninfo = "dbname=test host=/tmp/ user=postgres"; 

    /* Make a connection to the database */ 
    conn = PQconnectdb(conninfo); 

    /* Check to see that the backend connection was successfully made */ 
    if (PQstatus(conn) != CONNECTION_OK) 
    { 
     fprintf(stderr, "Connection to database failed: %s" 
       , PQerrorMessage(conn)); 
     exit_nicely(conn); 
    } 

    //do stuff here 
    errmsg = NULL;  // << HERE 
    res = PQexec(conn, "COPY cplusplustest(key1,col11,col12) from STDIN;"); 
    a = PQputCopyData(conn, buffer, strlen(buffer)); 
    b = PQputCopyEnd(conn, errmsg); 

    printf("Res=%p a=%d,b=%d\n", res, a, b); 

    if (errmsg) 
     printf("Failed:%s\n", errmsg); 
    else 
     printf("worked.\n"); 

    res = PQexec(conn, "COMMIT;");  // <<-- HERE 

    /* close the connection to the database and cleanup */ 
    PQfinish(conn); 

    return 0; 
} 
+0

謝謝秒。這樣做的工作。 – Exuro