在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 | |
jdbc | java | type 4 jdbc driver | |
libpqxx | c | c interface | |
node-postgres | javascript | node.js driver | |
npgsql | .net | .net data provider | |
pgtcl | tcl | - | |
pgtclng | tcl | - | |
pq | go | pure go driver for go’s database/sql | |
psqlodbc | odbc | odbc driver | |
psycopg | python | db api 2.0-compliant |
libpqxx 文档参考:
libpqxx 当前最新版本:7.7.0 下载地址:国内 国外
需要注意,使用libpqxx 7.x版本的需要c 17,本次测试还是使用旧版本的libpqxx(4.0.1)。
下面是一段c 语言连接postgresql并做查询的一段代码
[root@pgtest3 ~]# vi test.cpp
#include
#include
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: "<0].as<string>()<<endl;
cout<<"pg_is_in_recovery: "<1].as<string>()<<endl;
cout<<"current_database: "<2].as<string>()<<endl;
cout<<"current_user: "<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
^
compilation terminated.
编译安装最新版的libpqxx(当前最新版本:7.7.0),也会提示需要c 17,当前使用的是centos 7自带的c ,还得升级太麻烦,退而求其次还是使用老版本吧。
老版本libpqxx-4.0.1下载地址:
编译安装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
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
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【米乐app官网下载的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。