#include #include "pq-wrapper.h" static PGconn *conn; static PGresult *result; int my_PQconnectdb (void) { conn = PQconnectdb ("host=localhost port=5432 user=dcslow"); if (PQstatus(conn) != CONNECTION_OK) { fprintf (stderr, "ERROR: Could not connect to PostgreSQL server:\n %s", PQerrorMessage(conn)); return -1; } return 0; } PGresult *my_PQexec (const char *query, int returnsTuples) { if (result != NULL) { PQclear (result); } result = PQexec (conn, query); if ( PQresultStatus(result) != (returnsTuples ? PGRES_TUPLES_OK : PGRES_COMMAND_OK) ) { fprintf (stderr, "ERROR: PostgreSQL command failed:\n %s\n", query); char *msg = PQerrorMessage(conn); if (msg && *msg) { fprintf (stderr, " %s", msg); } else { fprintf (stderr, " (no error message returned)\n"); } return NULL; } return result; } void my_PQfinish (void) { if (result != NULL) { PQclear (result); } PQfinish (conn); return; }