windows下安装配置postgreSQL

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

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

(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", "5432", NULL, NULL, "postgres", "postgres", "123456");
    if (PQstatus(conn) != CONNECTION_OK) 
    {
        cout << "connection error" << endl;
        return -1;
    }
    else 
    {
        cout << "connection success" << endl;
        return 0;
    }
}

(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[256] = {"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 = 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;

 封装成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 = 0; i<tuple_num; ++i)
            {
                for (int j = 0; j<field_num; ++j)
                    cout << PQgetvalue(res, i, j) << " ";
                cout << endl;
            }
            //ClearQuery(res);
            return true;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/liyubo/p/9080501.html