C Client Library

  • Database connection functions
    • PGconn *PQconnectdb(const char *conninfo)
      • conninfo is a name=value string, eg: "username=swm dbname=test"
      • valid keywords: host, port, dbname, user, password, connection_timeout, options, tty, requiressl
    • ConnStatusType PQstatus(const PGconn *conn)
      • Status of connection
      • CONNECTION_OK, CONNECTION_BAD
      • CONNECTION_STARTED, CONNECTION_MADE, CONNECTION_AWAITING_RESPONSE, CONNECTION_AUTH_OK, CONNECTION_SETENV
    • void PQfinish(PGconn *conn)
      • Close a connection
    • void PQreset(PGconn *conn)
      • Disconnect and attempt to reconnect to a database
    • char *PQerrorMessage(const PGconn* conn);
      • Return the most recent error message for the given connection
01   #include "libpq-fe.h"
02   #include <stdio.h>
03
04   int main(void)
05   {
06      PGconn *conn;
07      conn = PQconnectdb("user=postgres dbname=a");
08
09      switch(PQstatus(conn)) {
10              case CONNECTION_BAD:
11                      fprintf(stderr,"Could not establish connection\n");
12                      exit(1);
13                      break;
14              case CONNECTION_STARTED:
15                      fprintf(stderr,"Connecting...\n");
16                      break;
17              case CONNECTION_OK:
18                      fprintf(stderr,"Connected\n");
19                      break;
20              default:
21                      break;
22      }
23      PQfinish(conn);
24   }
Prev

Next

Page 36