Linux下C++连接postgresql

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。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值