2017-05-11 3 views
1

J'essaie de passer plusieurs lignes à une table de test et j'ai de la difficulté à comprendre exactement comment cela se fait dans libpq.libpq comment passer des données en bloc

J'ai trouvé des commandes pour copier des données que je crois nécessaires, mais il n'y a pas d'exemples sur la façon de les utiliser. https://www.postgresql.org/docs/8.3/static/libpq-copy.html

Voici le code que j'ai trouvé, mais je reçois des erreurs de segmentation à la fonction PQputCopyEnd. Je suis assez perdu ici donc toute aide serait géniale.

/* 
* 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; 
} 

Répondre

0
  • Postgres 8.3 est très ancienne; s'il vous plaît envisager une version plus récente
  • l'argument errmsg à b = PQputCopyEnd(conn, errmsg); doit être réglé à NULL (il est entré pour libpq, ce qui indique que le client a avorté la copie) (the manual est encore assez vague, je suis d'accord.
  • J'ai enlevé le C++.
  • COMMIT devrait aller après la 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

Remerciez s. Cela fait le travail. – Exuro