Windows环境下C/C++访问PostgreSQL数据库
来源:互联网 发布:雅安电视台网络直播 编辑:程序博客网 时间:2024/05/21 05:08
转载自:https://segmentfault.com/a/1190000000628234
PostgreSQL是一款在Linux环境下应用十分广泛的轻量级关系型数据库,大家都听说过MySQL,却对PostgreSQL鲜有耳闻,它其实在性能、应用领域上和MySQL不相上下。网上关于Windows环境下C/C++访问PostgreSQL数据库的资料很少,文本分析了C/C++访问PostgreSQL数据库的过程。
Windows环境C/C++访问PostgreSQL主要有两种方式:利用Qt封装的数据库访问组件、利用PostgreSQL的API函数。使用Qt平台访问PostgreSQL的局限性很大,一旦脱离了访问组件,数据库就无法操作。使用数据库自带的API函数访问数据库具有较好的性能,但是API函数操作、理解比较难,网上相关资料少时需要阅读API文档。
1、环境配置
(1)文本使用的IDE是VS2013,我们需要配置包含目录(include)、库目录(lib)、链接器输入附加依赖(libpq.lib
);
(2)工程目录下需要加入4个dll文件(libeay32.dll
、libintl.dll
、libpq.dll
、ssleay32.dll
),这些文件都能在PostgreSQL安装目录下找到;
(3)工程cpp文件中加入头文件#include <libpq-fe.h>
,libpq-fe.h
头文件包含了API接口函数声明及注释,下面介绍的函数在libpq-fe.h
中都能找到。
2、连接数据库
(1)数据库连接函数
extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, const char *pgtty, const char *dbName, const char *login, const char *pwd);
返回值PGconn *
指针,即连接指针。如果你要对PQsetdbLogin
函数封装的话,记得将形参连接指针设成PGconn *&
引用类型,因为连接函数需要对连接指针修改,而不是修改对象!pghost
:主机地址,本机为127.0.0.1
或localhost
;pgport
:端口值,一般为5432;pgoptions
:额外选项,NULL
即可;pgtty
:NULL
即可;dbName
:数据库名;user
:用户名;pwd
:密码;
(2)错误显示函数extern char *PQerrorMessage(const PGconn *conn)
当连接有误时,可以使用PQerrorMessage
函数显示出错信息。
封装成ConnectToDB
函数:
bool ConnectToDB(PGconn *&conn,char *pghost,char *pgport,char *dbname,char *user,char *pwd){ //pgoptions、pgtty参数默认为NULL char *pgoptions,*pgtty; pgoptions=NULL; pgtty=NULL; conn=PQsetdbLogin(pghost,pgport,pgoptions,pgtty,dbname,user,pwd); if(PQstatus(conn)==CONNECTION_BAD) // or conn==NULL { cout<<"Connection db "<<dbname<<" failed!"<<endl; cout<<PQerrorMessage(conn)<<endl; return false; } else { cout<<"Connection db "<<dbname<<" success!"<<endl; return true; }}
3、执行SQL语句
执行SQL语句主要是增删改查,只有查询会返回有效记录集。
(1)SQL执行函数extern PGresult *PQexec(PGconn *conn, const char *query)
返回值PGresult *
:查询集指针;conn
:连接指针;query
:SQL语句;
(2)元组数函数extern int PQntuples(const PGresult *res)
返回值:查询集中记录数;res
:查询集指针;
(3)字段数函数extern int PQnfields(const PGresult *res)
返回值:每条记录中列数(字段数);res
:查询集指针;
(4)取值函数extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
返回值:查询集中每个位置的值;res
:查询集指针;tup_num
:行号,从0开始;field_num
:列号,从0开始;
封装成ExecSQL
函数:
bool ExecSQL(PGconn *conn,const char *sql){ PGresult *res=NULL; if(conn==NULL) { cout<<"Conn is null"<<endl; return false; } else { res=PQexec(const_cast<PGconn *>(conn),sql); if(res==NULL) { return false; } else { // 输出记录 int tuple_num=PQntuples(res); int field_num=PQnfields(res); for(int i=0;i<tuple_num;++i) { for(int j=0;j<field_num;++j) cout<<PQgetvalue(res,i,j)<<" "; cout<<endl; } ClearQuery(res); return true; } }}
4、关闭连接
(1)查询集清理函数extern void PQclear(PGresult *res)
res
:查询集指针;
(1)关闭连接函数extern void PQfinish(PGconn *conn)
conn
:连接指针;
5、错误查询
许多时候执行SQL语句后,数据表没有变化,程序也不报错,这种情况很难发现错误。我们需要使用PostgreSQL提供的errorMessage
和status
函数追踪程序变量的状态。
比如:
(1)PQerrorMessage
函数提供了PGconn连接指针的出错信息;
(2)PQresultErrorMessage
函数提供了PGresult查询集指针的出错信息;
(3)PQresultStatus
函数返回查询集指针的状态信息ExecStatusType
,这是个枚举enum
类型:
typedef enum{ PGRES_EMPTY_QUERY = 0, /* empty query string was executed */ PGRES_COMMAND_OK, /* a query command that doesn't return * anything was executed properly by the * backend */ PGRES_TUPLES_OK, /* a query command that returns tuples was * executed properly by the backend, PGresult * contains the result tuples */ PGRES_COPY_OUT, /* Copy Out data transfer in progress */ PGRES_COPY_IN, /* Copy In data transfer in progress */ PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the * backend */ PGRES_NONFATAL_ERROR, /* notice or warning message */ PGRES_FATAL_ERROR, /* query failed */ PGRES_COPY_BOTH, /* Copy In/Out data transfer in progress */ PGRES_SINGLE_TUPLE /* single tuple from larger resultset */} ExecStatusType;
有了这些工具,发现错不是难事!
- Windows环境下C/C++访问PostgreSQL数据库
- C程序访问PostgreSQL数据库
- Linux下PostgresQL数据库C语言接口:libpq (二)数据库访问
- 访问PostgreSQL:C语言接口
- Windows环境下的C程序设计环境
- Windows环境下的C程序设计环境
- windows下编译postgresql数据库
- Windows下编译PostgreSQL数据库
- Linux下使用C/C++访问数据库
- Linux下使用C/C++访问数据库
- Linux下C访问Mysql数据库
- Win7下 dev-c++用c语言访问mysql数据库环境配置
- Windows下c/c++开发环境配置
- Windows下搭建objective C开发环境
- windows 下 Objective C 编译环境搭建
- Windows下搭建Object-C环境
- Windows下搭建Objective-C开发环境
- Windows下搭建Object-C开发环境
- 数据定义语言DDL
- 数据库性能优化:程序操作优化
- Android基础入门教程——2.2.3 TableLayout(表格布局)
- 【转载】指针的指针
- mac中查看.ssh文件
- Windows环境下C/C++访问PostgreSQL数据库
- 前端知识体系及修炼攻略
- kafka伪集群搭建常见错误
- 【Java继承学习笔记系列】在继承关系下构造函数执行时的顺序
- c#中webservice函数如何实现重载
- 离散数学(集合论-图论-代数结构-组合数学-数理逻辑)——学习资料(更新.............)
- 什么是“堆”,"栈","堆栈","队列",它们的区别?
- 找出两个list集合中重复的部分
- 阿里云服务ECS(CentOS7) 用yum源安装nginx