author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 14 May 2009 17:31:45 +0200 | |
changeset 92 | 0bc2646daa82 |
parent 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
/** * database.c * Copyright (C) 2008 Markus Broeker */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libpq-fe.h> int main (int argc, char **argv) { char *conninfo; PGconn *conn; PGresult *res; int nFields; int i; int j; conninfo = "dbname=lightintron"; conn = PQconnectdb (conninfo); if (conn == NULL) printf ("ERROR\n"); else printf ("SUCCESS\n"); if (argc != 2) res = PQexec (conn, "select \"MNr\", \"Name\", \"Vorname\", \"Bemerkungen\" from \"Mitarbeiter\""); else res = PQexec (conn, argv[1]); /* * first, print out the attribute names */ nFields = PQnfields (res); for (i = 0; i < nFields; i++) printf ("%-15s", PQfname (res, i)); printf ("\n\n"); /* * next, print out the rows */ for (i = 0; i < PQntuples (res); i++) { for (j = 0; j < nFields; j++) { if (!strcmp (PQfname (res, j), "message")) printf ("\n"); printf ("%-15s", PQgetvalue (res, i, j)); } printf ("\n"); } PQclear (res); PQfinish (conn); return EXIT_SUCCESS; }