SOCI数据库操作库
来源:互联网 发布:手机fq软件 编辑:程序博客网 时间:2024/06/06 12:51
说明
SOCI库是一个数据操作的库,支持很多语言。SOCI库的接口已经支持了基本上所有的数据库。下文主要是C++下SOCI库的使用,搭配的是postgresql数据库。
安装
使用cmake工具(提前安装cmake)。
$ git clone https://github.com/SOCI/soci.git$ cd soci$ mkdir build $ cd build$ cmake -G "Unix Makefiles" -DWITH_BOOST=OFF -DWITH_POSTGRESQL=ON ../soci$ make$ make install
说明:git工具下载比较慢,建议直接去官网下载源码。
上面操作完毕之后,在build目录下会有一个include文件夹,里面包含soci-config.h头文件,它是一个soci配置文件 。
如果项目使用soci,那么
1. 将soci-config文件放到soci/include/soci目录下。
2. 将lib中的静态库和动态库放到指定的地方。
SOCI特点
- 以stream方式输入sql语句
- 通过into和use语法传递和解析参数
- 支持连接池,线程安全
SOCI的使用
将上面build/include/soci-config.h拷贝到soci/include/soci目录下。
将组合好的include目录拷贝到你到工程include目录,作为头文件。
此时include目录中有很多不必要的文件,例如其他数据库的头文件。
将build/lib中的.a或者.so文件拷贝到你的工程lib目录下,作为库文件,最好的方式将.so文件放到/usr/lib下。
测试代码:
// test.cpp #include <iostream>#include <string>#include "soci/soci.h"#include "soci/postgresql/soci-postgresql.h"#include <exception>using namespace std;using namespace soci;int main(){ try { int id = 1; string first_name = "pingsoli"; string second_name = "byy"; session sql("postgresql", "dbname=test user=postgres password=postgres"); sql << "insert into person values(:id, :first_name, :second_name)", use(id), use(first_name), use(second_name); } catch (exception &e) { cout << e.what() << endl; } return 0;}
说明:本人采用的postgresql数据库,为了确保程序正确运行,需提前运行数据库。在数据库运行的情况下,还要保证表是否存在,字段属性是否正确。
下面开始编译
$ g++ test.cpp -o test -I ./include -I /usr/local/postgresql/include -lsoci_core -lsoci_postgresql
注意:-I指定头文件的目录(编译时需要用到数据库的头文件)。上面是使用动态库,为了简单,直接将libsoci_core和libsoci_postgresql放到/usr/lib目录下。
编译时问题
cmake -G “Unix Makefiles” -DWITH_BOOST=OFF -DWITH_POSTGRESQL=ON -DPOSTGRESQL_INCLUDE_DIR=/usr/local/postgresql/include -DPOSTGRESQL_LIBRARIES=/usr/local/postgresql/lib ../soci出现如下警告信息:
WARNING: Target “soci_postgresql” requests linking to directory “/usr/local/postgresql/lib”. Targets may link only to libraries. CMake is dropping the item.
WARNING: Target “soci_postgresql_test” requests linking to directory “/usr/local/postgresql/lib”. Targets may link only to libraries. CMake is dropping the item.
WARNING: Target “soci_postgresql_test_static” requests linking to directory “/usr/local/postgresql/lib”. Targets may link only to libraries. CMake is dropping the item.
make之后产生错误信息:
Scanning dependencies of target soci_postgresql_test
[ 96%] Building CXX object backends/postgresql/test/CMakeFiles/soci_postgresql_test.dir/test-postgresql.cpp.o
[ 97%] Linking CXX executable ../../../bin/soci_postgresql_test
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQresultStatus'
PQgetvalue’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQclear'
PQresultErrorMessage’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQexec'
lo_lseek’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQfsize'
PQgetisnull’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQerrorMessage'
lo_open’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQexecPrepared'
PQftype’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to lo_close'
PQexecParams’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQfname'
PQconnectdb’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQstatus'
PQntuples’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQresultErrorField'
PQfformat’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to lo_read'
PQfinish’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQprepare'
lo_write’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQcmdTuples'
PQnfields’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
collect2: error: ld returned 1 exit status
backends/postgresql/test/CMakeFiles/soci_postgresql_test.dir/build.make:97: recipe for target ‘bin/soci_postgresql_test’ failed
make[2]: * [bin/soci_postgresql_test] Error 1
CMakeFiles/Makefile2:526: recipe for target ‘backends/postgresql/test/CMakeFiles/soci_postgresql_test.dir/all’ failed
make[1]: * [backends/postgresql/test/CMakeFiles/soci_postgresql_test.dir/all] Error 2
Makefile:138: recipe for target ‘all’ failed
make: * [all] Error 2
解决方法:去掉-DPOSTGRESQL_INCLUDE_DIR=/usr/local/postgresql/include -DPOSTGRESQL_LIBRARIES=/usr/local/postgresql/lib
参考
[1] SOCI基础:
http://blog.csdn.net/littlewhite1989/article/details/54691367
[2] SOCI官方文档:
http://soci.sourceforge.net/
[3] soci编译出现问题:
https://stackoverflow.com/questions/35392767/how-to-build-soci-with-postgresql
- SOCI数据库操作库
- 使用soci操作数据库
- C++数据库操作之SOCI
- SOCI、LiteSQL、POCO数据库访问类库对比
- SOCI、LiteSQL、POCO数据库访问类库对比
- SOCI、LiteSQL、POCO数据库访问类库对比
- 更简洁的C++数据库访问框架-soci
- soci库中使用oracle组函数导致bad_cast异常
- C++ SOCI
- soci-mysq编译
- mysql c++ 访问 soci
- soci开发环境搭建
- ubuntu16.04安装soci(一个可以封装访问oracle、mysql和postgresql等各种数据库的包)
- SOCI - The C++ Database Access Library
- Ubuntu下wxWidgets学生信息管理soci
- php数据库操作类库
- Mysql 数据库 库相关操作
- 操作数据库
- React-native中灵活运用虚拟对象传递参数
- Docker 的两类存储资源
- Ubuntu 下使用minicom 的配置过程
- 列表自定义表单(easyui+cookie)
- Java8的十大新特性
- SOCI数据库操作库
- 100个动态规划方程
- 十进制数转为十六进制字符串
- 尾递归消除
- 利用Python,四步掌握机器学习
- leetcode-Total Hamming Distance
- 【代码片-1】 jQuery源码学习(版本1.11)-事件处理-实例函数
- eric4 utf-8 问题
- ios环信-会话输入框等提示中文化