搜索
首页数据库mysql教程循序渐进PostgreSQL: 学习pqxx C API访问

习惯了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>
<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 << “连接成功!” << std::endl;</p>
<p>std::cout << conn.options()<<std::endl;</p>
<p>}</p>
<p>其他</p>
<p>{</p>
<p>std::cout << “出了点问题……哎呀”<< std::endl;</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 << res[0][0].as<int>() << std::endl;</p>
<p>}</p>
<p>

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具