#include int main(int argc, char **argv) { PGconn * conn; PGresult * res; int nfields, ntuples, i, j; conn = PQconnectdb("host=localhost dbname=foo " "user=foo password=bar"); res = PQexec(conn, "CREATE TABLE hello (message varchar(50))"); if(PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "%s\n", PQresultErrorMessage(res)); exit(1); } PQclear(res); res = PQexec(conn, "INSERT INTO hello VALUES ('Hello World!')"); if(PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "%s\n", PQresultErrorMessage(res)); exit(1); } PQclear(res); res = PQexec(conn, "SELECT * FROM hello"); if(PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "%s\n", PQresultErrorMessage(res)); exit(1); } nfields = PQnfields(res); ntuples = PQntuples(res); for(i = 0; i < ntuples; i++) { for(j = 0; j < nfields; j++) { printf("%s\n", PQgetvalue(res, i, j)); } } PQclear(res); PQfinish(conn); }