database.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:57:54 +0100
changeset 3 820ed7fb9314
child 8 96d16dfe787a
permissions -rw-r--r--
database, gauss, lotto, mem2swap, prog_limit moved to demos committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * $Id: main.c,v 1.1.1.1 2008-04-28 17:32:52 mbroeker Exp $
 * $Source: /development/c/database/main.c,v $
 *
 */

#include <stdio.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 0;
}