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進行舉報,并提供相關證據,一經查實,墨天輪將立刻刪除相關內容。




