Documentation
 
 
 

29.6. Usage and Example

To build (i.e., compile and link) a program using libpq the user needs to do the following:

  • Include the libpq-fe.h header file:

    #include <libpq-fe.h>

    If you failed to do that then you will normally get error messages from your compiler similar to

    foo.c: In function `main':
    foo.c:34: `PGconn' undeclared (first use in this function)
    foo.c:35: `PGresult' undeclared (first use in this function)
    foo.c:54: `CONNECTION_BAD' undeclared (first use in this function)
    foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function)
    foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function)

  • Failure to specify the correct option to the compiler will result in an error message such as

    testlibpq.c:8:22: libpq-fe.h: No such file or directory

  • When linking the final program, specify the option -lpq so that the libpq library gets pulled in, as well as the option -Ldirectory to point the compiler to the directory where the libpq library resides. (Again, the compiler will search some directories by default.) For maximum portability, put the -L option before the -lpq option.

    Error messages that point to problems in this area could look like the following.

    testlibpq.o: In function `main':
    testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin'
    testlibpq.o(.text+0x71): undefined reference to `PQstatus'
    testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'

    This means you forgot -lpq.

29.6.1. Example


#include "stdafx.h"
#include <libpq-fe.h>
#include <stdio.h>
#include <stdlib.h>
static void exit_nicely(PGconn *conn)
{   PQfinish(conn);
	exit(1);  
}
void main()
{ const char *conninfo;
    PGconn     *conn;
    PGresult   *res;
	PGconn*conn1;
	int i;
	int j;
	int nTuples;
	int nFields;
	//****setting the connection*******
	conninfo = "hostaddr =10.90.1.66 port=5444 dbname=edb user=enterprisedb      password= connect_timeout=5";
    /* Make a connection to the database */
    conn = PQconnectdb(conninfo);
    /* Check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s",
        PQerrorMessage(conn));
        exit_nicely(conn);
    }
	printf("Connection created Successfully\n");
	//***Executing Query//
// Table1 created 
res = PQexec(conn,"INSERT INTO emp VALUES (7769,'David','CLERK',7902,'17-DEC-80',800,NULL,20);");
if (PQresultStatus(res) != PGRES_COMMAND_OK)//checking for error
    {
        fprintf(stderr, "Insertion Failed: %s", PQerrorMessage(conn));
	PQclear(res);
	   exit_nicely(conn);
       }
else
	printf("Successfully inserted value in Table..... \n");
	// Checking the values in Test table
	res = PQexec(conn,"SELECT * FROM emp");//executing query
	if (PQresultStatus(res) != PGRES_TUPLES_OK)//checking for error
    {
        fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
	nFields = PQnfields(res);//getting number of fields
	nTuples = PQntuples(res);//getting number of tuples returned
           printf("Total Records : %d \n",nTuples);
	printf("Total Fields : %d \n",nFields);	
   /* next, print out the rows */
    for (i = 0; i < nTuples; i++)
    {
        for (j = 0; j < nFields; j++)
            printf("%s\t",PQgetvalue(res, i, j));
        printf("\n");
    }
// drop table
	res = PQexec(conn,"DELETE FROM emp WHERE empno=7769");
	if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "Employee record drop Failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
	printf("Employee record DROPPED Successfully\n");
/* close the connection to the database and cleanup */ 
    PQfinish(conn);
}

29.6.2. Output

The output that is obtained after executing the above piece of code is shown below:

 
 ©2004-2007 EnterpriseDB All Rights Reserved