#include <libpq-fe.h>
#include <stdio.h>
#include <stdlib.h>
void do_exit(PGconn *conn) {
PQfinish(conn);
exit(1);
}
int main() {
PGconn *conn = PQconnectdb(
"host=localhost user=admin password=quest port=8812 dbname=testdb");
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database failed: %s\n",
PQerrorMessage(conn));
do_exit(conn);
}
PGresult *res = PQexec(conn, "SELECT x FROM long_sequence(5);");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
printf("No data retrieved\n");
PQclear(res);
do_exit(conn);
}
int rows = PQntuples(res);
for (int i = 0; i < rows; i++) {
printf("%s\n", PQgetvalue(res, i, 0));
}
PQclear(res);
PQfinish(conn);
return 0;
}