1.下载 postgresql-10.4-1-windows-x64.exe 进行安装

2.环境配置
(1)文本使用的IDE是VS2010,我们需要配置包含目录(include)、库目录(lib)、链接器输入附加依赖(libpq.lib); 

(2)工程目录下需要加入4个dll文件(libeay32.dlllibintl-8.dlllibpq.dllssleay32.dll、libiconv-2.dll),这些文件都能在PostgreSQL安装目录下(D:\Program Files\PostgreSQL\10\bin)找到;
(3)工程cpp文件中加入头文件#include <libpq-fe.h>libpq-fe.h头文件包含了API接口函数声明及注释,下面介绍的函数在libpq-fe.h中都能找到。

3.连接数据库

(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:密码;

示例:

int connect()
{
//连接数据库的两种方式
//设置为自己postgresql的数据库名 用户名和密码
//conn = PQconnectdb("host=localhost port=5432 dbname=postgres user=postgres password=123456"); conn = PQsetdbLogin("localhost", "", NULL, NULL, "postgres", "postgres", "");
if (PQstatus(conn) != CONNECTION_OK)
{
cout << "connection error" << endl;
return -;
}
else
{
cout << "connection success" << endl;
return ;
}
}

(2)错误显示函数

extern char *PQerrorMessage(const PGconn *conn)
当连接有误时,可以使用PQerrorMessage函数显示出错信息。

示例:

cout<<PQerrorMessage(conn)<<endl;

(3)封装成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;
}
}

4、执行SQL语句
执行SQL语句主要是增删改查,只有查询会返回有效记录集。

示例表为:

(1)SQL执行函数

extern PGresult *PQexec(PGconn *conn, const char *query)
返回值PGresult *:查询集指针;
conn:连接指针;
query:SQL语句;

示例:

char query[] = {"SELECT a, b    FROM public.aaa "};
res = PQexec(conn, query);

(2)元组数函数

extern int PQntuples(const PGresult *res)
返回值:查询集中记录数;
res:查询集指针;

示例:

int tuple_num = PQntuples(res);

(3)字段数函数

extern int PQnfields(const PGresult *res)
返回值:每条记录中列数(字段数);
res:查询集指针;

示例:

int field_num = PQnfields(res);

(4)取值函数

extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
返回值:查询集中每个位置的值;
res:查询集指针;
tup_num:行号,从0开始;
field_num:列号,从0开始;

5、关闭连接
(1)查询集清理函数

extern void PQclear(PGresult *res)
res:查询集指针;

(2)关闭连接函数

extern void PQfinish(PGconn *conn)
conn:连接指针;

6、错误查询
许多时候执行SQL语句后,数据表没有变化,程序也不报错,这种情况很难发现错误。我们需要使用PostgreSQL提供的errorMessage和status函数追踪程序变量的状态。
比如:
(1)PQerrorMessage函数提供了PGconn连接指针的出错信息;

extern char *PQerrorMessage(const PGconn *conn);
conn:连接指针

(2)PQresultErrorMessage函数提供了PGresult查询集指针的出错信息;

extern char *PQresultErrorMessage(const PGresult *res);
res:查询集指针

(3)PQresultStatus函数返回查询集指针的状态信息ExecStatusType,这是个枚举enum类型:

extern ExecStatusType PQresultStatus(const PGresult *res);
res:查询集指针

ExecStatusType的枚举类型:

ypedef enum
{
PGRES_EMPTY_QUERY = , /* 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;

封装成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)
{
std::cout << PQresultErrorMessage(res) << endl; /* 打印失败原因 */
return PQresultStatus(res); /* 返回错误码 */
}
else
{
// 输出记录
int tuple_num = PQntuples(res);
int field_num = PQnfields(res);
for (int i = ; i<tuple_num; ++i)
{
for (int j = ; j<field_num; ++j)
cout << PQgetvalue(res, i, j) << " ";
cout << endl;
}
//ClearQuery(res);
return true;
}
}
}

windows下安装配置postgreSQL的更多相关文章

  1. 在windows下安装配置Ulipad

    在windows下安装配置Ulipad 今天推荐一款轻便的文本编辑器Ulipad,用来写一些小的Python脚本非常方便. Ulipad下载地址: https://github.com/limodou ...

  2. Windows下安装配置MongoDB

    Windows下安装配置MongoDB 一,介绍 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB ...

  3. (转)windows 下安装配置 Nginx 详解

    windows 下安装配置 Nginx 详解 本文转自https://blog.csdn.net/kingscoming/article/details/79042874 nginx功能之一可以启动一 ...

  4. QT学习之windows下安装配置PyQt5

    windows下安装配置PyQt5 目录 为什么要学习QT 命令行安装PyQt5以及PyQt5-tools 配置QtDesigner.PyUIC及PyRcc 为什么要学习QT python下与界面开发 ...

  5. RabbitMQ学习在windows下安装配置

    RabbitMQ学习一. 在windows下安装配置 1.下载并安装erlang,http://www.erlang.org/download.html,最新版是R15B01(5.9.1).由于我机器 ...

  6. Windows下安装配置免安装MySQL5.7服务器

      Windows下安装配置免安装MySQL5.7服务器 1.下载.解压安装包 从MySQL官方网站上下载mysql-5.7.19-winx64.zip 下载完成后,把安装包解压到D:\DevSoft ...

  7. Windows下安装配置MySQL

    Windows下安装配置MySQL的基本步骤 一.MySQL下载 MySQL官方下载地址https://dev.mysql.com/downloads/mysql/5.7.html#downloads ...

  8. Windows下安装配置ant

    1.ant安装 请从官网下载ant的*.zip格式的安装包, Windows建议下载*.zip版本, Linux建议下载*.gz版本. 2.配置环境变量 解压之后,在Windows中配置环境变量, 在 ...

  9. 2、Windows下安装配置Redis

    windows下redis软件开源安装包挂载到github上,下面将详细介绍如何在windows下安装redis服务器 下载地址:https://github.com/MSOpenTech/redis ...

随机推荐

  1. python开发环境搭建及numpy基本属性-【老鱼学numpy】

    目的 本节我们将介绍如何搭建python的开发环境以及numpy的基本属性,这样可以检验我们的numpy是否安装正确了. python开发环境的搭建 工欲善其事必先利其器,我用得比较顺手的是Intel ...

  2. Linux history显示时间/用户/ip的设置

    在使用linux服务器的时候发生一些不知道谁操作的问题,google一下说history命令可以查看到历史记录,用过之后发现还是不够详细,再google,原来可以自己设置history的显示. 记录设 ...

  3. window.open 打开全屏窗口

    window.open新打开页面为全屏状态,各个浏览器情况不一致. window.open   弹出新窗口的命令:     'page.html'   弹出窗口的文件名:     'newwindow ...

  4. 2019-2-21.NET中异常类(Exception)

    .NET中异常类(Exception) 异常:程序在运行期间发生的错误.异常对象就是封装这些错误的对象. try{}catch{}是非常重要的,捕获try程序块中所有发生的异常,如果没有捕获异常的话, ...

  5. Go语言基础(一)

    Go语言基础(一) 国庆体验一下大名鼎鼎的Go语言,IDE使用IEDA+Go插件,边敲代码边体会,感觉Go语言好酷 一.Hello World 和Java类似,go文件需要一个package包含,代码 ...

  6. helm-chart6,子chart 和全局值

    chart可以有称为子chart的依赖关系 关于子chart 1,子chart认为是"独立的",即子chart不能明确依赖于其父chart. 2,子chart无法访问其父项的值. ...

  7. jmeter 使用问题

    问题1:导入脚本失败,提示 解决:没有安装JMemter plugins manager插件,具体安装参考http://www.cnblogs.com/cxx1/p/7883820.html,第二步.

  8. react-native run-android时 SDK location not found.报错

    报错 原因 缺少local.properties文件(SDK location) 解决 方法一:在android Studio中打开项目android目录,会自动创建local.properties文 ...

  9. connect socket的超时设置

    最近项目中,有个需求是检测某ip地址是否是通的,使用了socket的connect函数.但是,当ip地址写错的话,connect就会一直阻塞在那里,大概2.3分钟才能返回连接失败.这对于用户来说是不可 ...

  10. [LeetCode] Similar RGB Color 相似的红绿蓝颜色

    In the following, every capital letter represents some hexadecimal digit from 0 to f. The red-green- ...