习惯了C 来开发的人,可能更倾向于使用c 库来访问PostgreSQL。libpqxx很早以前就推出了。目前已经到了4.0版。本文就简单的介绍
1. 简介
习惯了C 来开发的人,可能更倾向于使用c 库来访问PostgreSQL。libpqxx很早以前就推出了。目前已经到了4.0版。本文就简单的介绍如何使用它,,Linux平台编译相对简单,这里就介绍一下Windows平台下的使用。
libpqxx的下载地址:直接上这里
由于libpqxx对libpq的C-API进行了很好的封装,从而可以节省大量的编码时间。
2. 编译过程
将libpqxx解压到一固定目录。你需要提前编译好或安装好的客户端库,通常为了方便,这里假定你已经有一个PG的安装版或者解压缩版,在c:pgsql里头。以PG9.x为例。
1. 进入libpqxx源码目录,备份并修改win32common文件,将PGSQLSRC的值指向正确的地方:
这里是PGSQLSRC="c:pgsql"
后边还有很多目录位置需要调整,针对我们这种情况,通常都是保留安装版本的设置,如:LIBPQINC=$(PGSQLSRC)include, 而注释掉#LIBPQINC=$(PGSQLSRC)interfaceslibpq, 其它几个值都如此类推。
2. 拷贝一些编译相关的头文件
针对具体版本,9.0的,将configsample-headerslibpq9.0pqxx复制到include下边
针对VS2008的,将configsample-headerscompilerVisualStudio2008pqxx复制到include目录下边, 如果是VS2005,以此类推。
3. 编译
program files->VS2008-->Visual studio tools->Visual Studio 2008 Command Prompt, 进入命令行, 在此进入libpqxx的源代码根目录,执行:
nmake /f win32vc-libpqxx.mak ALL,
得到如下中间过程:
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
"ObjDllReleasebinarystring.obj" "ObjDllReleaseconnection.obj" "ObjDllRelea
seconnection_base.obj" "ObjDllReleasecursor.obj" "ObjDllReleasedbtransactio
n.obj" "ObjDllReleaseerrorhandler.obj" "ObjDllReleaseexcept.obj" "ObjDllRel
easefield.obj" "ObjDllReleaselargeobject.obj" "ObjDllReleasenontransaction.
obj" "ObjDllReleasenotification.obj" "ObjDllReleasenotify-listen.obj" "ObjD
llReleasepipeline.obj" "ObjDllReleaseprepared_statement.obj" "ObjDllRelease
result.obj" "ObjDllReleaserobusttransaction.obj" "ObjDllReleasestatement_par
ameters.obj" "ObjDllReleasestrconv.obj" "ObjDllReleasesubtransaction.obj" "
ObjDllReleasetablereader.obj" "ObjDllReleasetablestream.obj" "ObjDllRelease
tablewriter.obj" "ObjDllReleasetransaction.obj" "ObjDllReleasetransaction_ba
se.obj" "ObjDllReleasetuple.obj" "ObjDllReleaseutil.obj" "ObjDllReleaselib
pqxx.obj" /out:"liblibpqxx.dll" /implib:"liblibpqxx.lib"
Creating library liblibpqxx.lib and object liblibpqxx.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
"ObjDllReleasebinarystring.obj" "ObjDllReleaseconnection.obj" "ObjDllRelea
seconnection_base.obj" "ObjDllReleasecursor.obj" "ObjDllReleasedbtransactio
n.obj" "ObjDllReleaseerrorhandler.obj" "ObjDllReleaseexcept.obj" "ObjDllRel
easefield.obj" "ObjDllReleaselargeobject.obj" "ObjDllReleasenontransaction.
obj" "ObjDllReleasenotification.obj" "ObjDllReleasenotify-listen.obj" "ObjD
llReleasepipeline.obj" "ObjDllReleaseprepared_statement.obj" "ObjDllRelease
result.obj" "ObjDllReleaserobusttransaction.obj" "ObjDllReleasestatement_par
ameters.obj" "ObjDllReleasestrconv.obj" "ObjDllReleasesubtransaction.obj" "
ObjDllReleasetablereader.obj" "ObjDllReleasetablestream.obj" "ObjDllRelease
tablewriter.obj" "ObjDllReleasetransaction.obj" "ObjDllReleasetransaction_ba
se.obj" "ObjDllReleasetuple.obj" "ObjDllReleaseutil.obj" "ObjDllReleaselib
pqxx.obj" /out:"liblibpqxx.dll" /implib:"liblibpqxx.lib"
Creating library liblibpqxx.lib and object liblibpqxx.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
“ObjDllReleasebinarystring.obj” “ObjDllReleaseconnection.obj” “ObjDllRelea
seconnection_base.obj" "ObjDllReleasecursor.obj" "ObjDllReleasdbtransactio
n.obj" "ObjDllReleaseerrorhandler.obj" "ObjDllRelease except.obj" "ObjDllRel
easefield.obj" "ObjDllReleaselargeobject.obj" "ObjDllReleasenontransaction。
obj" "ObjDllReleasenotification.obj" "ObjDllReleasenotify-listen.obj" "ObjD
llReleasepipeline.obj" "ObjDllReleaseprepared_statement.obj" "ObjDllRelease
结果.obj" "ObjDllReleaserobusttransaction.obj" "ObjDllReleasestatement_par
ameters.obj" "ObjDllReleasestrconv.obj" "ObjDllReleasesubtransaction.obj" "
ObjDllReleasetablereader.obj" "ObjDllReleasetablestream.obj" "ObjDllRelease
tablewriter.obj" "ObjDllReleasetransaction.obj" "ObjDllReleasetransaction_ba
se.obj" "ObjDllReleasetuple.obj" "ObjDllReleaseutil.obj" "ObjDllReleaselib
pqxx.obj" /out:"liblibpqxx.dll" /implib:"liblibpqxx.lib"
创建库 liblibpqxx.lib 和对象 liblibpqxx.exp 最终会在lib目录下生成:
[纯文本]查看纯文本?
D:Projectshisql.svntrunklearninglibpqxx-4.0>dir/b/s lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpq.dll
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpq.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.dll
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.dll.manifest
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.exp
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.dll
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.dll.manifest
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.exp
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.ilk
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.pdb
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx_static.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx_staticD.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0libmsdia80.dll
D:Projectshisql.svntrunklearninglibpqxx-4.0>dir/b/s lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpq.dll
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpq.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.dll
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.dll.manifest
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.exp
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.dll
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.dll.manifest
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.exp
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.ilk
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxxD.pdb
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx_static.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0liblibpqxx_staticD.lib
D:Projectshisql.svntrunklearninglibpqxx-4.0libmsdia80.dll
下边来看看最简单的示例:
iihero=# 创建用户 foo 密码 'foo1';
创建角色
[sql] 查看 plaincopyprint?
iihero=>创建表 t(id int 主键, col2 varchar(32));
注意: CREATE TABLE / PRIMARY KEY 将要为表 "t" 创建隐含索引 "t_pkey"
创建表
iihero=>编码
GBK
iihero=> insert into t value(1 ,'不同类型的列表');
插入 0 1
iihero=>从 t 中选择*;
id | 列2
--------------------------------
1 | 不同类型的列表
(1行记录)
iihero=>创建表 t(id int 主键, col2 varchar(32));
注意: CREATE TABLE / PRIMARY KEY 将要为表 "t" 创建隐含索引 "t_pkey"
创建表
iihero=>编码
GBK
iihero=> insert into t value(1 ,'不同类型的列表');
插入 0 1
iihero=>从 t 中选择*;
id | 列2
--------------------------------
1 | 不同类型的列表
(1行记录)
将编译出来后的include、lib目录提取到固定的目录,和你原来的postgresql的include和lib目录,可以合到一起。给开发使用。
下边是一个最简单的示例:
[cpp] 查看纯文本?
#include <iostream> <p>#include <pqxx></pqxx></p> <p>int main()</p> <p>{</p> <p>pqxx::connection conn("dbname=iihero hostaddr=127.0.0.1 user=foo 密码=foo1" );</p> <p>if(conn.is_open())</p> <p>{</p> <p>std::cout </p> <p>std::cout </p> <p>}</p> <p>其他</p> <p>{</p> <p>std::cout </p> <p>}</p> <p>pqxx::work w(conn);</p> <p>pqxx::result res = w.exec("SELECT 1");</p> <p>w.commit();</p> <p>std::cout () </p> <p>}</p> <p>