summaryrefslogtreecommitdiffstats
path: root/db_demo/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'db_demo/main.c')
-rw-r--r--db_demo/main.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/db_demo/main.c b/db_demo/main.c
new file mode 100644
index 0000000..c28f273
--- /dev/null
+++ b/db_demo/main.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <time.h>
+#include <libpq-fe.h>
+
+#include "main.h"
+
+int main(int argc, char *argv[]) {
+ PGconn *conn;
+ char SQL_query[512];
+ PGresult *res;
+ int ntuples, nfields;
+ int i, j;
+
+ conn = PQsetdbLogin(PGHOST, PGPORT, PGOPTIONS, PGTTY, DBNAME, LOGIN, PWD);
+
+
+ if(CONNECTION_BAD == PQstatus(conn))
+ {
+ fprintf(stderr, "Connection to database '%s' failed.\n %s", DBNAME, PQerrorMessage(conn));
+ PQfinish(conn);
+ return 1;
+ }
+
+ sprintf(SQL_query, "SELECT * FROM testicle");
+ res = PQexec(conn, SQL_query);
+
+ if(res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK)
+ {
+ printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn));
+ }
+ else
+ {
+ ntuples = PQntuples(res);
+ nfields = PQnfields(res);
+
+ for (i = 0; i < ntuples; i++)
+ {
+ for (j = 0; j < nfields; j++)
+ {
+ printf("Query result: %s - %s\n", PQfname(res, j), PQgetvalue(res, i, j));
+ }
+ }
+ }
+
+ PQclear(res);
+ PQfinish(conn);
+
+ return 0;
+}