Skip to content. | Skip to navigation

This Top Brown Viewlet registered to qPloneSkinBrio product
Sections
You are here: Home News libpq Examples for C++

 Subscribe in a reader

libpq Examples for C++

by Bryan Hinton last modified Apr 18, 2008 11:18 AM
— filed under:

PGresult *res = PQexec(conn, "BEGIN"); /* begin transaction */ if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, insertstmt); ...

const char *conninfo;
PGconn *conn;

conninfo = "dbname = dbname user=uname";
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK) {
    fprintf(stderr, "Connection to database failed: %s",
    PQerrorMessage(conn));
    exit_nicely(conn);
}

PGresult *res;
res = PQexec(conn, somequery); /* execute query */
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
   fprintf(stderr, "Query failed: %s", PQerrorMessage(conn));
   PQclear(res);
   exit_nicely(conn);
}

if (j = PQntuples(res)) { /* process query */
   for (int i = 0; i < j; i++) {
      someint = atoi(PQgetvalue(res, i, 0));
      anotherint = atof(PQgetvalue(res, i, 1));
   }
   PQclear(res);
}

res = PQexec(conn, "BEGIN"); /* begin transaction */
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
   fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
   PQclear(res);
   exit_nicely(conn);
}
PQclear(res);

res = PQexec(conn, insertstmt);

if (PQresultStatus(res) != PGRES_COMMAND_OK) { /* insert */

   fprintf(stderr, "INSERT command failed: %s", PQerrorMessage(conn));

   PQclear(res);

   exit_nicely(conn);

}

PQclear(res);

 

res = PQexec(conn, "END"); /* end transaction */

PQclear(res);

PQfinish(conn);

Document Actions