/** Returns number of records returned by given command. */ int xdb_row_count(char *command) { int errno = 0, tuples = 0; PGconn *conn = xdb_connect(); PQsendQuery(conn, command); PGresult *res = PQgetResult(conn); errno = PQresultStatus(res); if (errno > PGRES_TUPLES_OK) { fprintf(stderr, "error: DATABASE command failed: %i\n", errno); return 0; } tuples = PQntuples(res); PQfinish(conn); return tuples; }
/** * Executes the given SQL command through the Postgres library (libpq) * * @author Martin Turon * * @return Error code from Postgres after executing command * * @version 2004/8/8 mturon Initial version * */ int xdb_execute(char *command) { int errno = 0; PGconn *conn = xdb_connect(); PQsendQuery(conn, command); PGresult *res = PQgetResult(conn); printf("%s\n", command); while (res != NULL) { errno = PQresultStatus(res); if (errno > 1) fprintf(stderr, "error: INSERT command failed: %i\n", errno); res = PQgetResult(conn); PQclear(res); } /* close the connection to the database and cleanup */ PQfinish(conn); return errno; }