Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <libpq-fe.h>
- #include <stdlib.h>
- PGresult * run_query(PGconn *conn, char * query)
- {
- PGresult *res = PQexec(conn, query);
- ExecStatusType resStatus = PQresultStatus(res);
- printf("Query Status: %s\n", PQresStatus(resStatus));
- if (resStatus != PGRES_TUPLES_OK) {
- fprintf(stderr, "Error while executing query: %s\n", PQerrorMessage(conn));
- PQclear(res);
- PQfinish(conn);
- exit(1);
- }
- return res;
- }
- void print_query(PGresult * res)
- {
- int rows = PQntuples(res);
- int cols = PQnfields(res);
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- printf("%s\t", PQgetvalue(res, i, j));
- }
- puts("");
- }
- }
- int main(void)
- {
- char *conninfo = "dbname=postgres user=postgres password=postgres host=localhost port=5432";
- PGconn *conn = PQconnectdb(conninfo);
- if (PQstatus(conn) != CONNECTION_OK) {
- fprintf(stderr, "Error while connetinning in postgresSQL.\n");
- PQfinish(conn);
- exit(1);
- }
- char *query = "select * from nivel";
- PGresult *res = run_query(conn, query);
- print_query(res);
- PQclear(res);
- PQfinish(conn);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement