linux安装好了postgresql之后,可以通过c++来连接,而c++连接postgresql需要libpqxx依赖库,我通过yum install libpqxx安装成功,c++源文件引用<pqxx/pqxx>竟然没有生效,提示pqxx/pqxx: No such file or directory,只能通过下载源码编译安装:
wget http://pqxx.org/download/software/libpqxx/libpqxx-4.0.1.tar.gz
tar -xzf libpqxx-4.0.1.tar.gz
cd libpqxx-4.0.1
./configure
make && make install
这样安装之后,pqxx库就安装上了,c++需要包含的文件就在/usr/local/include目录下。
libpqxx提供了操作postgresql的api。 简单说明一下这些api:
- connection:连接。需要指定主机、端口、用户名、密码、数据库。
- work:支持事务的执行器,exec(sql)表示执行sql语句,用来做增删改操作。
- nontransaction:不支持事务的执行器,也提供exec(sql)执行sql语句,用来做查询操作。
- result :结果集,执行查询nontransaction.exec(sql)返回的结果。
我们在postgresql数据库mydb下准备一张用户表xx_user
编写c++代码,我们测试一下连接数据库,向xx_user表中插入数据,查询数据三个功能。
测试连接:
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc,char* argv[]){
try{
connection conn("dbname=mydb user=postgres password=postgres \
hostaddr=127.0.0.1 port=5432");
if(conn.is_open()){
cout<<"connect to postgresql successfully. dbname="<<conn.dbname()<<endl;
}else{
cout<<"connect to postgresql fail."<<endl;
return 1;
}
conn.disconnect();
}catch(const std::exception &e){
cerr<<e.what()<<endl;
return 1;
}
return 0;
}
编译并运行:g++ main.cpp -lpqxx -lpq 或者g++ main.cpp -lpqxx -lpq -o main
向表中插入数据:
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc,char* argv[]){
const char* sql;
try{
connection conn("dbname=mydb user=postgres password=postgres \
hostaddr=127.0.0.1 port=5432");
if(conn.is_open()){
cout<<"connect to postgresql successfully. dbname="<<conn.dbname()<<endl;
}else{
cout<<"connect to postgresql fail."<<endl;
return 1;
}
work tnx(conn);
sql = "insert into xx_user(id,name,mobile,birth) values (1,'aaa','15011186301',current_date),(2,'bbb','15910909520',current_date)";
tnx.exec(sql);
tnx.commit();
cout<<"insert record ok."<<endl;
conn.disconnect();
}catch(const std::exception &e){
cerr<<e.what()<<endl;
return 1;
}
return 0;
}
编译并运行:
运行成功,查看数据库中的数据:
查询数据:
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc,char* argv[]){
const char* sql;
try{
connection conn("dbname=mydb user=postgres password=postgres \
hostaddr=127.0.0.1 port=5432");
if(conn.is_open()){
cout<<"connect to postgresql successfully. dbname="<<conn.dbname()<<endl;
}else{
cout<<"connect to postgresql fail."<<endl;
return 1;
}
// work tnx(conn);
// sql = "insert into xx_user(id,name,mobile,birth) values (1,'aaa','15011186301',current_date),(2,'bbb','15910909520',current_date)";
// tnx.exec(sql);
// tnx.commit();
nontransaction ntx(conn);
sql = "select * from xx_user";
result r(ntx.exec(sql));
for(result::const_iterator c=r.begin(); c!=r.end(); ++c){
cout<<"id="<<c[0].as<int>()<<endl;
cout<<"name="<<c[1].as<string>()<<endl;
cout<<"mobile="<<c[2].as<string>()<<endl;
cout<<"birth="<<c[3].as<string>()<<endl;
}
//cout<<"insert record ok."<<endl;
conn.disconnect();
}catch(const std::exception &e){
cerr<<e.what()<<endl;
return 1;
}
return 0;
}
编译并运行:
到这里,c++连接postgresql并执行相关操作就介绍完了,主要用到了libpqxx库,他提供了c++操作postgresql的一系列api。