C language client library

Database Connection

Examples
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   }
01   #include "libpq-fe.h"
02   #include <stdio.h>
03
04   #include <sys/time.h>
05   #include <sys/types.h>
06   #include <unistd.h>
07
08
09   int main(void)
10   {
11      int i;
12      PGconn *conn;
13       struct timeval tv;
14      conn = PQconnectStart("user=postgres dbname=a");
15      tv.tv_sec = 0;
16      tv.tv_usec = 50;
17
18      i = 0;
19      while(i<100) {
20              switch(PQconnectPoll(conn)) {
21                      case PGRES_POLLING_ACTIVE:
22                              fprintf(stderr,"polling active");
23                              break;
24                      case PGRES_POLLING_WRITING:
25                              fprintf(stderr,"polling writing");
26                              select(1,NULL,(fd_set *)PQsocket(conn),NULL,&tv);
27                              break;
28                      case PGRES_POLLING_READING:
29                              fprintf(stderr,"polling reading");
30                              select(1,(fd_set *)PQsocket(conn),NULL, NULL,&tv);
31                              break;
32                      case PGRES_POLLING_OK:
33                              fprintf(stderr,"connection ready");
34                              break;
35                      case PGRES_POLLING_FAILED:
36                              fprintf(stderr,"failed"); exit(1);
37              }
38              i++;
39      }
40      PQfinish(conn);
41   }


Query Execution


Retrieving SELECT results

01   #include "libpq-fe.h"
02   #include <string.h>
03
04   int main(void)
05   {
06      PGconn *conn;
07      PGresult *res;
08      int i,j;
09      conn = PQconnectdb("user=postgres dbname=a");
10      if(PQstatus(conn) == CONNECTION_BAD) {
11              fprintf(stderr,"Connection failed: %s\n",PQerrorMessage(conn));
12              exit(1);
13      }
14
15      res = PQexec(conn,"SELECT id,firstname,lastname from employees;");
16      if(PQresultStatus(res) != PGRES_TUPLES_OK) {
17              fprintf(stderr,"Query failed: %s\n",PQresultErrorMessage(res));
18              exit(1);
19      }
20
21      for(i=0;i<PQntuples(res);i++) {
22              for(j=0;j<PQnfields(res);j++) {
23                      printf("%s: %s ",PQfname(res,j),PQgetvalue(res,i,j));
24              }
25              printf("\n");
26      }
27      PQclear(res);
28      PQfinish(conn);
29      return(0);
30   }
Asynchronous Query Processing Threading

Prev

Next