database.c
changeset 3 820ed7fb9314
child 8 96d16dfe787a
equal deleted inserted replaced
2:97beb75e5ac7 3:820ed7fb9314
       
     1 /**
       
     2  * $Id: main.c,v 1.1.1.1 2008-04-28 17:32:52 mbroeker Exp $
       
     3  * $Source: /development/c/database/main.c,v $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <string.h>
       
     9 #include <libpq-fe.h>
       
    10 
       
    11 int main (int argc, char **argv)
       
    12 {
       
    13     char *conninfo;
       
    14     PGconn *conn;
       
    15     PGresult *res;
       
    16     int nFields;
       
    17     int i;
       
    18     int j;
       
    19 
       
    20     conninfo = "dbname=lightintron";
       
    21 
       
    22     conn = PQconnectdb (conninfo);
       
    23     if (conn == NULL)
       
    24         printf ("ERROR\n");
       
    25     else
       
    26         printf ("SUCCESS\n");
       
    27 
       
    28     if (argc != 2)
       
    29         res = PQexec (conn, "select \"MNr\", \"Name\", \"Vorname\", \"Bemerkungen\" from \"Mitarbeiter\"");
       
    30     else
       
    31         res = PQexec (conn, argv[1]);
       
    32 
       
    33     /*
       
    34      * first, print out the attribute names
       
    35      */
       
    36     nFields = PQnfields (res);
       
    37     for (i = 0; i < nFields; i++)
       
    38         printf ("%-15s", PQfname (res, i));
       
    39     printf ("\n\n");
       
    40 
       
    41     /*
       
    42      * next, print out the rows
       
    43      */
       
    44 
       
    45     for (i = 0; i < PQntuples (res); i++) {
       
    46         for (j = 0; j < nFields; j++) {
       
    47             if (!strcmp (PQfname (res, j), "message"))
       
    48                 printf ("\n");
       
    49             printf ("%-15s", PQgetvalue (res, i, j));
       
    50         }
       
    51         printf ("\n");
       
    52     }
       
    53 
       
    54     PQclear (res);
       
    55 
       
    56     PQfinish (conn);
       
    57 
       
    58     return 0;
       
    59 }