windows下C++访问pgsql(1)
(2015-06-23 22:20:54)这里顺便我也可以介绍一个关于pgsql的几个工具,这些都可以在pgsql的官网进行下载,很方便。。
http://www.enterprisedb.com/products-services-training/pgdownload:很全面的东西
一路next下去的话,就可以完成,里面可能会有几个参数需要你设置,因为里面包含了pgsql 服务器 cmdline 以及我们的大象(pgAdmin III);
大象的话是windows下的界te面客户端工具,很方便。
然后在我们的安装目录下可以看到data目录,上面我说到了这个其实可以当做本地服务器进行的,那样的话我们的sql数据其实就存在了data目录下。,
最重要的也就是今天我要用到的就是include以及lib目录,为什么呢??
因为今天我们在windows下面链接pgsql的话,其实就是调用大象客户端的代码,include和lib目录下就是一些接口函数,是不是很明白了呢!
直接开始了:::
1、环境配置
(1)文本使用的IDE是VS2010,我们需要配置包含目录(include)、库目录(lib)、链接器输入附加依赖(libpq.lib
);
(2)工程目录下需要加入4个dll文件(libeay32.dll
、libintl.dll
、libpq.dll
、ssleay32.dll
),这些文件都能在PostgreSQL安装目录下找到;
(3)工程cpp文件中加入头文件#include
,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 *
指针,即连接指针。
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) {
3、执行SQL语句 执行SQL语句主要是增删改查,只有查询会返回有效记录集。 (1)SQL执行函数char *pgoptions,*pgtty; pgoptions=NULL; pgtty=NULL; conn=PQsetdbLogin(pghost,pgport,pgoptions,pgtty,dbname,user,pwd); if(PQstatus(conn)==CONNECTION_BAD) { cout<<"Connection db "<<dbname<<" failed!"<<endl; cout<<PQerrorMessage(conn)<<endl; return false; } else { cout<<"Connection db "<<dbname<<" success!"<<endl; return true; } } 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(conn),sql);
if(res==NULL)
{
return false;
}
else
{
int tuple_num=PQntuples(res);
int field_num=PQnfields(res);
for(int i=0;i
{
for(int j=0;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,
PGRES_COMMAND_OK,
PGRES_TUPLES_OK,
PGRES_COPY_OUT,
PGRES_COPY_IN,
PGRES_BAD_RESPONSE,
PGRES_NONFATAL_ERROR,
PGRES_FATAL_ERROR,
PGRES_COPY_BOTH,
PGRES_SINGLE_TUPLE
} ExecStatusType;
就这样吧,以后可能会增加其他几种链接的方式