Step-by-Step PostgreSQL: Learn about pqxxC ++ API access

Source: Internet
Author: User
People who are used to C ++ development may prefer to use the c ++ library to access PostgreSQL. Libpqxx was launched a long time ago. It is now in version 4.0. This article briefly introduces

People who are used to C ++ development may prefer to use the c ++ library to access PostgreSQL. Libpqxx was launched a long time ago. It is now in version 4.0. This article briefly introduces

1. Introduction

People who are used to C ++ development may prefer to use the c ++ library to access PostgreSQL. Libpqxx was launched a long time ago. It is now in version 4.0. This article briefly introduces how to use it. compiling on the Linux platform is relatively simple. Here we will introduce how to use it on the Windows platform.

Libpqxx: click here

Because libpqxx has encapsulated libpq C-API well, it can save a lot of coding time.

2. compilation process

Extract libpqxx to a fixed directory. You need to compile or install the client library in advance. Generally, for convenience, assume that you already have an install or decompress version of PG, which is in c: \ pgsql. Take PG9.x as an example.

1. Go to the libpqxx source code directory, back up and modify the win32 \ common file, and point the PGSQLSRC value to the correct place:

Here is PGSQLSRC = "c: \ pgsql"

There are still many directory locations to be adjusted. In this case, we usually keep the installation version settings, such as LIBPQINC =$ (PGSQLSRC) \ include, comment out # LIBPQINC =$ (PGSQLSRC) \ interfaces \ libpq, and so on.

2. copy some compilation-related header files.

For specific versions, copy config \ sample-headers \ libpq \ 9.0 \ pqxx To the bottom of include

For VS2008, copy config \ sample-headers \ compiler \ VisualStudio2008 \ pqxx to the include directory. For VS2005, and so on.

3. Compile

Program files-> VS2008 --> Visual studio tools-> Visual Studio 2008 Command Prompt, enter the Command line, enter the source code root directory of libpqxx, and execute:

Nmake/f win32 \ vc-libpqxx.mak ALL,

The intermediate process is as follows:

Link.exe kernel32.lib ws2_32.lib advapi32.lib/nologo/dll/machine: I386
Shell32.lib secur32.lib wldap32.lib/libpath: "C: \ hisql-x86-2.0.1" \ lib libpq. lib
"ObjDllRelease \ binarystring. obj" "ObjDllRelease \ connection. obj" "ObjDllRelea
Se \ connection_base.obj "" ObjDllRelease \ cursor. obj "" ObjDllRelease \ dbtransactio
N. obj "" ObjDllRelease \ errorhandler. obj "" ObjDllRelease \ release T. obj "" ObjDllRel
Struct \ field. obj "" ObjDllRelease \ largeobject. obj "" ObjDllRelease \ nontransaction.
Obj "" ObjDllRelease \ notification. obj "" ObjDllRelease \ notify-listen.obj "" ObjD
LlRelease \ pipeline. obj "" ObjDllRelease \ prepared_statement.obj "" ObjDllRelease \
Result. obj "" ObjDllRelease \ robusttransaction. obj "" ObjDllRelease \ statement_par
Ameters. obj "" ObjDllRelease \ strconv. obj "" ObjDllRelease \ subtransaction. obj ""
ObjDllRelease \ tablereader. obj "" ObjDllRelease \ tablestream. obj "" ObjDllRelease \
Tablewriter. obj "" ObjDllRelease \ transaction. obj "" ObjDllRelease \ transaction_ba
Se. obj "" ObjDllRelease \ tuple. obj "" ObjDllRelease \ util. obj "" ObjDllRelease \ lib
Pqxx. obj "/out:" lib \ libpqxx. dll "/implib:" lib \ libpqxx. lib"
Creating library lib \ libpqxx. lib and object lib \ libpqxx. exp

[Plain] view plaincopyprint?

Link.exe kernel32.lib ws2_32.lib advapi32.lib/nologo/dll/machine: I386

Shell32.lib secur32.lib wldap32.lib/libpath: "C: \ hisql-x86-2.0.1" \ lib libpq. lib

"ObjDllRelease \ binarystring. obj" "ObjDllRelease \ connection. obj" "ObjDllRelea

Se \ connection_base.obj "" ObjDllRelease \ cursor. obj "" ObjDllRelease \ dbtransactio

N. obj "" ObjDllRelease \ errorhandler. obj "" ObjDllRelease \ release T. obj "" ObjDllRel

Struct \ field. obj "" ObjDllRelease \ largeobject. obj "" ObjDllRelease \ nontransaction.

Obj "" ObjDllRelease \ notification. obj "" ObjDllRelease \ notify-listen.obj "" ObjD

LlRelease \ pipeline. obj "" ObjDllRelease \ prepared_statement.obj "" ObjDllRelease \

Result. obj "" ObjDllRelease \ robusttransaction. obj "" ObjDllRelease \ statement_par

Ameters. obj "" ObjDllRelease \ strconv. obj "" ObjDllRelease \ subtransaction. obj ""

ObjDllRelease \ tablereader. obj "" ObjDllRelease \ tablestream. obj "" ObjDllRelease \

Tablewriter. obj "" ObjDllRelease \ transaction. obj "" ObjDllRelease \ transaction_ba

Se. obj "" ObjDllRelease \ tuple. obj "" ObjDllRelease \ util. obj "" ObjDllRelease \ lib

Pqxx. obj "/out:" lib \ libpqxx. dll "/implib:" lib \ libpqxx. lib"

Creating library lib \ libpqxx. lib and object lib \ libpqxx. exp

Link.exe kernel32.lib ws2_32.lib advapi32.lib/nologo/dll/machine: I386

Shell32.lib secur32.lib wldap32.lib/libpath: "C: \ hisql-x86-2.0.1" \ lib libpq. lib

"ObjDllRelease \ binarystring. obj" "ObjDllRelease \ connection. obj" "ObjDllRelea

Se \ connection_base.obj "" ObjDllRelease \ cursor. obj "" ObjDllRelease \ dbtransactio

N. obj "" ObjDllRelease \ errorhandler. obj "" ObjDllRelease \ release T. obj "" ObjDllRel

Struct \ field. obj "" ObjDllRelease \ largeobject. obj "" ObjDllRelease \ nontransaction.

Obj "" ObjDllRelease \ notification. obj "" ObjDllRelease \ notify-listen.obj "" ObjD

LlRelease \ pipeline. obj "" ObjDllRelease \ prepared_statement.obj "" ObjDllRelease \

Result. obj "" ObjDllRelease \ robusttransaction. obj "" ObjDllRelease \ statement_par

Ameters. obj "" ObjDllRelease \ strconv. obj "" ObjDllRelease \ subtransaction. obj ""

ObjDllRelease \ tablereader. obj "" ObjDllRelease \ tablestream. obj "" ObjDllRelease \

Tablewriter. obj "" ObjDllRelease \ transaction. obj "" ObjDllRelease \ transaction_ba

Se. obj "" ObjDllRelease \ tuple. obj "" ObjDllRelease \ util. obj "" ObjDllRelease \ lib

Pqxx. obj "/out:" lib \ libpqxx. dll "/implib:" lib \ libpqxx. lib"

Creating library lib \ libpqxx. lib and object lib \ libpqxx. exp will be generated in the lib directory:

[Plain] view plaincopyprint?

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0> dir/B/s lib

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpq. dll

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpq. lib

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. dll

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. dll. manifest

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. exp

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. dll

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. dll. manifest

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxxD. exp

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. ilk

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. pdb

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxx_static.lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxx_staticD.lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ msdia80.dll

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0> dir/B/s lib

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpq. dll

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpq. lib

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. dll

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. dll. manifest

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. exp

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxx. lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. dll

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. dll. manifest

D: \ Projects \ hisql. svn \ trunk \ learning \ libpqxx-4.0 \ lib \ libpqxxD. exp

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. ilk

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxxD. pdb

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxx_static.lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ libpqxx_staticD.lib

D: \ Projects \ hisql. svn \ trunk \ study \ libpqxx-4.0 \ lib \ msdia80.dll

Let's take a look at the simplest example:

Iihero = # create user foo password 'foo1 ';

CREATE ROLE

[SQL] view plaincopyprint?

Iihero => create table t (id int primary key, col2 varchar (32 ));

Note: create table/primary key will CREATE an implicit index for the TABLE "t" "t_pkey"

CREATE TABLE

Iihero => \ encoding

GBK

Iihero => insert into t values (1, 'list of different type ');

INSERT 0 1

Iihero => select * from t;

Id | col2

---- + ----------------

1 | different types of lists

(1 line record)

Iihero => create table t (id int primary key, col2 varchar (32 ));

Note: create table/primary key will CREATE an implicit index for the TABLE "t" "t_pkey"

CREATE TABLE

Iihero => \ encoding

GBK

Iihero => insert into t values (1, 'list of different type ');

INSERT 0 1

Iihero => select * from t;

Id | col2

---- + ----------------

1 | different types of lists

(1 line record)

Extract the compiled include and lib directories to a fixed Directory, which can be combined with your original postgresql include and lib directories. For development and use.

Below is a simple example:

[Cpp] view plaincopyprint?

#include 
 
  

#include

int main()

{

pqxx::connection conn("dbname=iihero hostaddr=127.0.0.1 user=foo password=foo1" );

if(conn.is_open())

{

std::cout << "Connection succesful!" << std::endl;

std::cout << conn.options()<

}

else

{

std::cout << "Something went wrong... oops" << std::endl;

}

pqxx::work w(conn);

pqxx::result res = w.exec("SELECT 1");

w.commit();

std::cout << res[0][0].as () << std::endl;

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.