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

张玉龙 2021-11-08
2939

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.

其他语言客户端接口:

Name Language Comments Website
DBD::Pg Perl Perl DBI driver https://metacpan.org/release/DBD-Pg
JDBC Java Type 4 JDBC driver https://jdbc.postgresql.org/
libpqxx C++ C++ interface https://pqxx.org/
node-postgres JavaScript Node.js driver https://node-postgres.com/
Npgsql .NET .NET data provider https://www.npgsql.org/
pgtcl Tcl - https://github.com/flightaware/Pgtcl
pgtclng Tcl - https://sourceforge.net/projects/pgtclng/
pq Go Pure Go driver for Go’s database/sql https://github.com/lib/pq
psqlODBC ODBC ODBC driver https://odbc.postgresql.org/
psycopg Python DB API 2.0-compliant https://www.psycopg.org/

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 current_database: postgres current_user: postgres
「喜欢文章,快来给作者赞赏墨值吧」
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论