demo.c

00001 
00002 #include <stdio.h>
00003 #include <pgsql/libpq-fe.h>
00004 
00005 main () 
00006 {
00007    FILE *fh;
00008    PGconn *conn;
00009    int i, row, col, rc, nrows, ncols;
00010    char * dbName = "gnc_bogus";
00011    PGresult *result;
00012 
00013    conn = PQsetdbLogin (NULL, NULL, NULL, NULL, dbName, NULL, NULL);
00014 
00015    if (CONNECTION_BAD == PQstatus(conn))
00016    {
00017       printf("Fatal Error: Connection to database '%s' failed:\n",
00018              dbName ? dbName : "(null)");
00019       printf("\t%s", PQerrorMessage(conn));
00020       PQfinish (conn);
00021       exit (1);
00022    }
00023 
00024    fh = fopen("/tmp/pgsql.trace","w");
00025    PQtrace(conn, fh);
00026 
00027    rc = PQsendQuery (conn, "SELECT * FROM gncAccount;");
00028    if (!rc) 
00029    {
00030       printf("Fatal Error: send query failed:\n");
00031       printf("\t%s", PQerrorMessage(conn));
00032       PQfinish (conn);
00033       exit (1);
00034    }
00035 
00036 
00037    i = 0;
00038    do {
00039       ExecStatusType status;
00040       int iacc, idesc;
00041 
00042       result = PQgetResult(conn);
00043       if (!result) break;
00044  
00045       status = PQresultStatus(result);
00046       
00047       if ((PGRES_COMMAND_OK != status) && 
00048           (PGRES_TUPLES_OK  != status)) 
00049       {
00050          printf ("Error: failed to get result to query\n");
00051          PQclear(result);
00052          PQfinish (conn);
00053          exit (2);
00054       }
00055       nrows = PQntuples (result);
00056       ncols = PQnfields(result);
00057       printf ("Info: query result %d has %d rows and %d cols\n", 
00058               i, nrows, ncols);
00059 
00060       iacc = PQfnumber (result, "accountName");
00061       idesc = PQfnumber (result, "description");
00062 
00063      printf ("accountName ===   description\n");
00064       for (row=0; row<nrows; row++) {
00065           printf ("%s ==== %s\n",
00066                   PQgetvalue(result, row, iacc),
00067                   PQgetvalue(result, row, idesc));
00068       }
00069 
00070       i++;
00071    } while (result);
00072 
00073    PQfinish (conn);
00074 }

Generated on Mon Sep 8 05:03:45 2008 for GnuCash by  doxygen 1.5.2