Good C/C++ connector library for PostgreSQL

libpq++ is one provide very good connector for PostgreSQL

SQLAPI++ is a C++ library for accessing multiple SQL databases (Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC, SQLite).

Abstract Database Connector is a C/C++ library for making connections to several databases (MySQL, mSQL, PostgreSQL, Interbase, Informix, BDE, ODBC). It runs on Linux, UNIX, BeOS, and Windows, and a dynamic driver loader for ELF OSes is under development

Navicat is Nice GUI tool for PostgrSQL


I wrote a wrapper around libpq for our needs. I was a long time Zeoslib (http://sourceforge.net/projects/zeoslib/) but they were riped with problems last I used them, didn't support cached datasets and plain slow.

libpq is very, very fast.

I simply download the Windows version of Postgres, copy all the DLLs to a lib directory and export the calls, include the .h and link accordingly.

I realize this is very low level but I can't emphasize enough the performance increase I'm realizing as a result.

Our application is an accounting/ERP type business application with fairly large install base some with fairly significant concurrent many user base (60, 100 connections)... This has served us very well... You can reply back if you want more details on how we wrap libpq and handle cached updates.

UPDATE:  

From a request here are the steps to wrap libpq or make use of it directly under windows.

First, to level set, I use Embarcadero RAD XE these days so the commands that follow are the command line tools that ship with RAD XE. You also need to make sure that your command line tools are in the PATH environment variable if not already. If using Visual Studio, then you will have to work out the equivalent commands. Basically I'm creating a .lib file out of a .DLL

TEST.C is a minimalist test code I wrote to make sure I understood how to use libpq and also to test my success.

1.  Put all the DLLs into a directory and copy the include directory.  

You do not need to install PostgreSQL using the MSI build to get these DLLs, although that would work too. I copied the DLLs from the binary build. The .H files were also taken from the binary build as well. So my directory looks like this:

  include\
     libpq-fe.h
     postgres_ext.h
  libeay32.dll
  libiconv-2.dll
  libintl-8.dll
  libpq.dll
  ssleay32.dll
  zlib1.dll



2.  Create an import library against LIBPQ.DLL as follows:
    implib -c -a libpq.lib libpq.dll

should now have a libpq.lib file in the same directory as your DLLs.

3.  build the test program (or any program) as follows:
    bcc32 test.c -l libpq.lib

test.c

#include <stdio.h>
#include "include/libpq-fe.h"


char *db       = "mydatabasename";
char *dbserver = "hostname";
char *uname    = "username";
char *pass     = "password";

char *SQL      = "select * from public.auditlog;";
// char *SQL      = "select userid, stationid from public.auditlog";

char     buff[200];
PGconn   *dbconn;
PGresult *res;

void main(void)
{
int nFields, i, j;

  printf("Attempting to Connect to Database Server:\n");
  printf("Database: %s\n", db);
  printf("Server  : %s\n", dbserver);

  sprintf(buff, "dbname=%s host=%s port=5432 user=%s password=%s",
                 db, dbserver, uname, pass);

  dbconn = PQconnectdb(buff);

  if( PQstatus(dbconn) != CONNECTION_OK )
    printf("Connection Failed: %s\n", PQerrorMessage(dbconn) );
  else
  {
    printf("Connected Successfully!\n");

    sprintf(buff, "BEGIN; DECLARE my_portal CURSOR FOR %s", SQL);

    res = PQexec(dbconn, buff);
    if( PQresultStatus(res) != PGRES_COMMAND_OK )
    {
      printf("Error executing SQL!: %s\n", PQerrorMessage(dbconn) );
      PQclear(res);
    }
    else
    {
      PQclear(res);
      res = PQexec(dbconn, "FETCH ALL in my_portal" );

      if( PQresultStatus(res) != PGRES_TUPLES_OK )
      {
        printf("ERROR, Fetch All Failed: %s", PQerrorMessage(dbconn) );
        PQclear(res);
      }
      else
      {
        nFields = PQnfields(res);

        // Print out the field names
        for(i=0; i<nFields; i++ )
          printf("%-15s", PQfname(res, i) );

        printf("\n");

        // Print out the rows
        for(i=0; i<PQntuples(res); i++)
        {
          for(j=0; j<nFields; j++)
            printf("%-15s", PQgetvalue(res, i, j) );

          printf("\n");
        }

        res = PQexec(dbconn, "END" );
        PQclear(res);
      }
    }

  }


  PQfinish(dbconn);
}

Now to access a PostgreSQL system I simply copy the libpq.lib file into any new RAD-XE project and add the libpq.lib to the project. I have wrapped the libpq into a database transport driver that sort of separates my database access code away.

The following screen shot shows a RAD-XE project called ptidb that, in turn, uses libpq to provide PostgreSQL support. I also support SQLite except with SQLite I just compile the database directly.

Then I simply ship the DLLs, listed above, along with my final product making sure the DLLs wind up in the same directory as my product.

This should get you going. If you're also interested in the C++ wrapping I do, let me know and I'll post another update with some of it.

enter image description here


Take a look at SOCI. Is an open source lib under Boost Software License (one of the most non-restrictive licenses at all).

This lib is designed especially for C++ with the idea of generic programming and type safety in mind.

SOCI Site

Tags:

C++

Postgresql