SOCI数据库操作库

说明

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 
#include 
#include "soci/soci.h"
#include "soci/postgresql/soci-postgresql.h"
#include 

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'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQgetvalue’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQclear'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQresultErrorMessage’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQexec'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
lo_lseek’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQfsize'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQgetisnull’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQerrorMessage'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
lo_open’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQexecPrepared'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQftype’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to lo_close'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQexecParams’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQfname'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQconnectdb’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQstatus'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQntuples’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQresultErrorField'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQfformat’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to lo_read'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQfinish’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQprepare'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
lo_write’
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to PQcmdTuples'
../../../lib/libsoci_postgresql.so.3.2.3: undefined reference to
PQnfields’
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


你可能感兴趣的:(c++)