添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

使用C++语言连接PostgreSQL数据库

原创 张玉龙 2021-11-08
7508

PostgreSQL的客户端接口

在PostgreSQL发行版中只包含两个客户端接口: libpq 和 ECPG

  • libpq is included because it is the primary C language interface, and because many other client interfaces are built on top of it.
  • ECPG is included because it depends on the server-side SQL grammar, and is therefore sensitive to changes in PostgreSQL itself.
  • 其他语言客户端接口:

    Language Comments Website

    C++语言连接PostgreSQL数据库

    libpqxx 文档参考: https://libpqxx.readthedocs.io/en/latest/index.html
    libpqxx 当前最新版本:7.7.0 下载地址:国内 https://gitee.com/mirrors/libpqxx/tree/master 国外 https://github.com/jtv/libpqxx
    需要注意,使用libpqxx 7.x版本的需要C++ 17,本次测试还是使用旧版本的libpqxx(4.0.1)。

    下面是一段C++语言连接PostgreSQL并做查询的一段代码

    [root@pgtest3 ~]# vi test.cpp #include <iostream> #include <pqxx/pqxx> using namespace std; using namespace pqxx; int main(int argc, char* argv[]) const char* sql; try{ connection conn("dbname=postgres user=postgres password=postgres hostaddr=192.168.58.10 port=5432"); nontransaction ntx(conn); sql = "select inet_server_addr(),pg_is_in_recovery(),current_database(),current_user"; result r(ntx.exec(sql)); for(result::const_iterator c=r.begin(); c!=r.end(); ++c){ cout<<"inet_server_addr: "<<c[0].as<string>()<<endl; cout<<"pg_is_in_recovery: "<<c[1].as<string>()<<endl; cout<<"current_database: "<<c[2].as<string>()<<endl; cout<<"current_user: "<<c[3].as<string>()<<endl; conn.disconnect (); }catch (const std::exception &e){ cerr << e.what() << std::endl; return 1; return 0;

      如果不安装libpqxx,编译会报出以下错误:

    [root@pgtest3 ~]# g++ test.cpp -lpqxx -lpq test.cpp:2:22: fatal error: pqxx/pqxx: No such file or directory #include <pqxx/pqxx> compilation terminated.

      编译安装最新版的libpqxx(当前最新版本:7.7.0),也会提示需要C++ 17,当前使用的是CentOS 7自带的C++,还得升级太麻烦,退而求其次还是使用老版本吧。
      老版本libpqxx-4.0.1下载地址:
      http://pqxx.org/download/software/libpqxx/libpqxx-4.0.1.tar.gz

      编译安装libpqxx-4.0.1时遇到以下报错:

    [root@pgtest3 ~]# cd /enmo/soft/libpqxx-4.0.1 [root@pgtest3 libpqxx-4.0.1]# ./configure config.status: executing configitems commands Traceback (most recent call last): File "./tools/splitconfig", line 154, in <module> generate_config(original_header, items_map, publication, factor) File "./tools/splitconfig", line 118, in generate_config % (publication, factor)) TypeError: a bytes-like object is required, not 'str'

      百度搜了一下,全是跟Python报这个错相关的,当前系统安装了Python3,转念一想这么老的libpqxx版本,应该不会用到Python3,改会Python2编译成功:

    [root@pgtest3 libpqxx-4.0.1]# rm -f /usr/bin/python [root@pgtest3 libpqxx-4.0.1]# ln -s /usr/bin/python2 /usr/bin/python [root@pgtest3 libpqxx-4.0.1]# ./configure [root@pgtest3 libpqxx-4.0.1]# make [root@pgtest3 libpqxx-4.0.1]# make install

      试着再次编译一下代码,又报错了

    [root@pgtest3 ~]# g++ test.cpp -lpqxx -lpq /usr/bin/ld: cannot find -lpq collect2: error: ld returned 1 exit status

      找不到libpq库,那就安装一下 postgresql-devel 再次编译执行成功,返回查询结果

    [root@pgtest3 ~]# yum install -y postgresql-devel [root@pgtest3 ~]# g++ test.cpp -lpqxx -lpq [root@pgtest3 ~]# ./a.out inet_server_addr: 192.168.58.10 pg_is_in_recovery: f