【构建依赖库】libpqxx 库安装及使用
如果迭代对象的类型是非平凡类型(non-trivial type),并且未定义相应的迭代器类型,可能会出现引用丢失的问题。,可以从官方网站下载源代码进行编译安装。实现的一个 C++ 封装库,在 C++ 程序中可以更方便地操作 PostgreSQL 数据库。要解决这个问题,你需要安装 PostgreSQL 客户端开发包。具体方法取决于你的操作系统和安装方式。那么这通常意味着你的系统缺少 Postgr
目录
2、解决Can't find the main PostgreSQL client header
1、下载源码包
libpqxx
是基于 PostgreSQL C 客户端库 libpq
实现的一个 C++ 封装库,在 C++ 程序中可以更方便地操作 PostgreSQL 数据库。官网:
GitHub - jtv/libpqxx: The official C++ client API for PostgreSQL.The official C++ client API for PostgreSQL. Contribute to jtv/libpqxx development by creating an account on GitHub.https://github.com/jtv/libpqxx如果你的系统没有提供预编译的包,或者需要使用更高版本的 libpqxx
,可以从官方网站下载源代码进行编译安装。在 Linux 或 macOS 系统上,你可以按照以下步骤进行编译安装:
# 下载源码包
wget https://github.com/jtv/libpqxx/archive/7.4.1.tar.gz
# 解压源码包
tar xzf 7.4.1.tar.gz
# 进入源码目录
cd libpqxx-7.4.1
# 编译和安装:指定安装位置
./configure --prefix=/home/cjs/algorithm/pq
遇到问题:
checking libpq-fe.h usability... no
checking libpq-fe.h presence... no
checking for libpq-fe.h... no
configure: error:
Can't find the main PostgreSQL client header, libpq-fe.h. Are you sure the
libpq headers are installed correctly, and that we've got the right path?
2、解决Can't find the main PostgreSQL client header
如果你在编译或安装 PostgreSQL 应用程序时遇到了以下错误:
Can't find the main PostgreSQL client header, libpq-fe.h.
那么这通常意味着你的系统缺少 PostgreSQL 客户端开发包,其中包括 libpq-fe.h
头文件。
要解决这个问题,你需要安装 PostgreSQL 客户端开发包。具体方法取决于你的操作系统和安装方式。
如果你使用的是 Ubuntu 或 Debian 发行版,请尝试运行以下命令来安装:
sudo apt-get install libpq-dev
即可解决问题。然后再次执行:
./configure --prefix=/home/cjs/algorithm/pq
3、构建安装
make
sudo make install
结果展示:
生成.a文件
4、构建使其生成动态库
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON ..
sudo make install DESTDIR=/home/cjs/algorithm/pqd
生成.so文件
5、psql连接测试
psql数据库:
建立qt的C++工程:
main.cpp
#include <iostream>
#include "pqxx/pqxx"
using namespace std;
int main()
{
std::string conn_string = "hostaddr=填写目的ip地址 port=30061 dbname=test user=postgres password=fjf";
pqxx::connection conn(conn_string);
pqxx::work txn(conn);
pqxx::result result = txn.exec("SELECT * FROM fan");
txn.commit();
// Process results
for(auto row_it = result.begin(); row_it != result.end(); row_it++)
{
std::cout << "id: " << row_it[0].as<int>() << std::endl;
}
conn.close();
return 0;
}
结果展示:
注意事项
如果使用下面的代码:
// Process results
for (auto row : result)
{
std::cout << "id: " << row[0].as<int>() << std::endl;
}
出现警告:
missing reference in range-for with non trivial type
原因分析:
如果迭代对象的类型是非平凡类型(non-trivial type),并且未定义相应的迭代器类型,可能会出现引用丢失的问题。
更多推荐
所有评论(0)