1. Introduction
People who are used to c ++ development may prefer to use the c ++ library to access PG. 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.
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
It will be generated in the lib directory:
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
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:
# Include <iostream>
# Include <pqxx/pqxx>
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 () <std: endl;
} Www.2cto.com
Else
{
Std: cout <"Something went wrong... oops" <std: endl;
}
Pqxx: work w (conn );
Pqxx: result res = w.exe c ("SELECT 1 ");
W. commit ();
Std: cout <res [0] [0]. as <int> () <std: endl;
}
Note that you must set libpq. lib, libpqxx. lib, and header files. If there is a problem with the dynamic library connection method, you can try to use the static library of libpqxx.