PostgreSQL数据库
时间:2021-04-02 13:25:24
收藏:0
阅读:0
https://www.yiibai.com/postgresql/
PostgreSQL教程
https://www.yiibai.com/postgresql/postgresql_c_cpp.html
C/C++连接PostgreSQL数据库
https://www.jianshu.com/p/8f5b6b3d7b38
libpqxx 库安装及使用
1: libpqxx 安装
// 下载库文件 -> 生成共享库(这一步对后面CMakeList 有用)-> 安装
wget http://pqxx.org/download/software/libpqxx/libpqxx-4.0.1.tar.gz
tar -xzvf libpqxx-4.0.1.tar.gz
cd libpqxx-4.0.1
./configure --prefix=/usr/local --enable-shared (这一步很重要,生成共享库 )
make clean
make
sudo make install
- 验证是否安装成功(查看是否有 libpqxx.so文件)
cd /usr/local/lib/
ll | grep libpqxx.so
或者直接: locate libpqxx.so
2: CMakeLists.txt 调用share 库
cmake_minimum_required(VERSION 3.0.0)
project(test_pgsql VERSION 0.1.0)
add_executable(test_pgsql main.cpp)
target_link_libraries(test_pgsql libpqxx.so )
3: 测试代码
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[])
{
try
{
connection db(
"dbname=vslam_map user=postgres password=postgres hostaddr=127.0.0.1 port=5432");
if (db.is_open())
{
cout << "Opened database successfully: " << db.dbname() << endl;
}
else
{
cout << "Can‘t open database" << endl;
return 1;
}
db.disconnect();
}
catch (const std::exception& e)
{
cerr << e.what() << std::endl;
return 1;
}
}
作者:jerryfive
链接:https://www.jianshu.com/p/8f5b6b3d7b38
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论(0)