当前位置:网站首页>Using soci to connect Oracle with PostgreSQL and SQLite on rhel8
Using soci to connect Oracle with PostgreSQL and SQLite on rhel8
2022-07-27 07:28:00 【hawanglc】
Software installation
install oracle client
Connect oracle need oracle Client software or oracle Server software . In this example oracle Client as an example .
rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
After installation , Environment variables need to be configured
[[email protected] ~]$ cat .bash_profile
#.bash_profile
#Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#User specific environment and startup programs
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib
ORACLE_HOME=/usr/lib/oracle/12.2/client64
export ORACLE_HOME
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}/lib
export TNS_ADMIN=$ORACLE_HOME/network/admin
install postgrersql
rpm -ivh postgresql12-docs-12.7-2PGDG.rhel8.x86_64.rpm postgresql12-libs-12.7-2PGDG.rhel8.x86_64.rpm postgresql12-12.7-2PGDG.rhel8.x86_64.rpm postgresql12-server-12.7-2PGDG.rhel8.x86_64.rpm
postgresql12-contrib-12.7-2PGDG.rhel8.x86_64.rpm
in addition , You need a development kit postgresql12-devel-12.7-2PGDG.rhel8.x86_64.rpm, Install this development kit , Need various third-party dependencies , In order to save trouble , Here is only the rpm Unpack the bag .
rpm2cpio postgresql12-devel-12.7-2PGDG.rhel8.x86_64.rpm | cpio -div
install boost_1_53_0( Optional packaging )
cd /home/baby/Downloads/boost_1_53_0/
./bootstrap.sh
./b2 install --with=all
install soci
cd /home/baby/Downloads/soci-4.0.2
mkdir build
cd bulid
The following command , Is to compile the connection oracle、postgresl and sqlite3 Dynamic library . Referring to official documents : http://soci.sourceforge.net/doc/release/4.0/installation/
cmake -G "Unix Makefiles" -DWITH_BOOST=OFF -DSOCI_CXX11=ON -DWITH_ORACLE=ON -DORACLE_INCLUDE_DIR=/u01/app/oracle/product/11.2.0/xe/rdbms/public/ -DORACLE_LIBRARIES=/u01/app/oracle/product/11.2.0/xe/lib -DSOCI_ORACLE=ON -DWITH_POSTGRESQL=ON -DPOSTGRESQL_INCLUDE_DIR=/usr/pgsql-12/include -DPOSTGRESQL_LIBRARY=/usr/pgsql-12/lib -DPOSTGRESQL_LIBRARIES=pq -DSOCI_POSTGRESQL=ON -DWITH_SQLITE3=ON -DSQLITE3_INCLUDE_DIR=/usr/include/ -DSQLITE3_LIBRARIES=/usr/lib64/ ..
make
Be careful : The path in the above command needs to be correct . Then in /home/baby/Downloads/soci-4.0.2/build/lib The following files are generated in :
libsoci_core.a [email protected] [email protected] libsoci_odbc.so.4.0.2* libsoci_postgresql.a [email protected]
[email protected] [email protected] libsoci_mysql.so.4.0.2* libsoci_oracle.a [email protected] [email protected]
[email protected] libsoci_empty.so.4.0.2* libsoci_odbc.a [email protected] [email protected] libsoci_sqlite3.so.4.0.2*
libsoci_core.so.4.0.2* libsoci_mysql.a [email protected] [email protected] libsoci_postgresql.so.4.0.2*
libsoci_empty.a [email protected] [email protected] libsoci_oracle.so.4.0.2* libsoci_sqlite3.a
Test code
Connect oracle Example
//============================================================================
// Name : soci_oracle.cpp
/*
g++ -std=c++0x -I/home/baby/tools/soci-4.0.2/include/soci -I/home/baby/tools/soci-4.0.2/include -I/home/baby/tools/soci-4.0.2/build/include -g -L/home/baby/tools/soci-4.0.2/build/lib -lsoci_oracle -lsoci_core -o soci_oracle soci_oracle.cpp
*/
//============================================================================
#include <soci.h>
#include <iostream>
#include <string>
using namespace soci;
using std::string;
void create_table()
{
try
{
soci::session sql("oracle", "service=xe user=baby password=baby1234");
sql << "create table Person(id number, name varchar2(50))";
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void drop_table()
{
try
{
soci::session sql("oracle", "service=xe user=baby password=baby1234");
sql << "drop table person";
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void insert_table()
{
try
{
soci::session sql("oracle", "service=xe user=baby password=baby1234");
int id(100);
string name("Bjarne");
sql << "insert into Person values (:ID, :NAME)", use(id), use(name);
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void select_table()
{
try
{
soci::session sql("oracle", "service=xe user=baby password=baby1234");
int id2;
string name2;
sql << "select id, name from Person", into(id2), into(name2);
std::cout << name2 << " has id " << id2 << std::endl;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void select_table_conn_pool()
{
try
{
soci::connection_pool pool(10);
for (int i = 0; i < 10; i++)
{
soci::session &sql = pool.at(i);
sql.open("oracle", "service=xe user=baby password=baby1234");
}
soci::session one_session(pool);
int id2;
string name2;
one_session << "select id, name from Person", into(id2), into(name2);
std::cout << name2 << " has id " << id2 << std::endl;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
/*
CREATE TABLE TEST_BLOB
(ID NUMBER(5),
NAME varchar2(100),
BLOBATTR blob);
* */
void select_blob()
{
try
{
soci::session sql("oracle", "service=xe user=baby password=baby1234");
std::string sql_str(
"select name, BLOBATTR from TEST_BLOB where ID=:id");
soci::blob blob_attr(sql);
std::string name;
int id = 1;
sql << sql_str, into(name), into(blob_attr), soci::use(id);
std::cout << " name " << name << std::endl;
std::cout << " blob_attr.get_len " << blob_attr.get_len() << std::endl;
std::cout << " blob_attr.get_len " << blob_attr.get_len() << std::endl;
char attr[20] = {0};
blob_attr.read_from_start(attr, blob_attr.get_len());
for (int i = 0; i < blob_attr.get_len(); i++)
{
std::cout << "value is " << attr[i] << std::endl;
}
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void update_blob()
{
try
{
soci::session sql("oracle", "service=xe user=baby password=baby1234");
std::string sql_str(
"select name, BLOBATTR from TEST_BLOB where ID=1 for update");
soci::blob blob_attr(sql);
std::string name;
sql << sql_str, into(name), into(blob_attr);
std::cout << " name " << name << std::endl;
std::cout << " blob_attr.get_len " << blob_attr.get_len() << std::endl;
std::cout << " blob_attr.get_len " << blob_attr.get_len() << std::endl;
char attr[20] = {0};
blob_attr.read_from_start(attr, blob_attr.get_len());
for (int i = 0; i < blob_attr.get_len(); i++)
{
std::cout << "value is " << attr[i] << std::endl;
}
attr[19] = 'D';
blob_attr.write_from_start(attr, 20);
sql.commit();
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cout << "parameter error" << std::endl;
std::cout << "C create table" << std::endl;
std::cout << "I insert table" << std::endl;
std::cout << "S select table" << std::endl;
std::cout << "SP select table using connection pool" << std::endl;
std::cout << "D drop table" << std::endl;
std::cout << "SB select blob" << std::endl;
return 1;
}
std::string cmd(argv[1]);
if (cmd == "C")
{
create_table();
}
if (cmd == "I")
{
insert_table();
}
if (cmd == "S")
{
select_table();
}
if (cmd == "SP")
{
select_table_conn_pool();
}
if (cmd == "D")
{
drop_table();
}
if (cmd == "SB")
{
select_blob();
}
if (cmd == "UB")
{
update_blob();
}
return 0;
}
At compile time , Use C++11 include The file should contain .lib The connection path should be configured correctly , The required libraries are libsoci-oracle,libsoci-core
During operation , Need configuration LD_LIBRARY_PATH The path of , hold libsoci-oracle,libsoci-core And so on .
Connect postgresql Example
//============================================================================
// Name : soci_postgresql.cpp
/*
g++ -std=c++0x -I/home/baby/tools/soci-4.0.2/include/soci -I/home/baby/tools/soci-4.0.2/include -I/home/baby/tools/soci-4.0.2/build/include -g -L/home/baby/tools/soci-4.0.2/build/lib -L/usr/pgsql-12/lib -lpq -lsoci_postgresql -lsoci_core -o soci_postgresql soci_postgresql.cpp
*/
//============================================================================
#include <soci.h>
#include <iostream>
#include <string>
using namespace soci;
using std::string;
void create_table()
{
try
{
soci::session sql("postgresql",
"dbname=postgres user=postgres password=postgres");
sql << "create table Person(id integer, name text)";
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void drop_table()
{
try
{
soci::session sql("postgresql",
"dbname=postgres user=postgres password=postgres");
sql << "drop table person";
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void insert_table()
{
try
{
soci::session sql("postgresql",
"dbname=postgres user=postgres password=postgres");
int id(100);
string name("Bjarne");
sql << "insert into Person values (:ID, :NAME)", use(id), use(name);
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void select_table()
{
try
{
soci::session sql("postgresql",
"dbname=postgres user=postgres password=postgres");
int id2;
string name2;
sql << "select id, name from Person", into(id2), into(name2);
std::cout << name2 << " has id " << id2 << std::endl;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
/*
CREATE TABLE TEST_BLOB
(ID integer,
NAME text,
BLOBATTR bytea);
* */
void select_blob()
{
try
{
soci::session sql("postgresql",
"dbname=postgres user=postgres password=postgres");
std::string sql_str(
"select name, bytea_attr from tb_bytea_test where ID=1");
//
std::string name;
std::string bytea;
// soci::blob bytea(sql);
sql << sql_str, into(name), into(bytea);
std::cout << " name " << name << std::endl;
std::cout << " bytea " << bytea << " length of bytea is "
<< bytea.size() << std::endl;
/*
std::cout << " blob_attr.get_len " << bytea.get_len() << std::endl;
std::cout << " blob_attr.get_len " << bytea.get_len() << std::endl;
char attr[20] = { 0 };
bytea.read_from_start(attr, bytea.get_len());
for (int i = 0; i < bytea.get_len(); i++) {
std::cout << "value is " << attr[i] << std::endl;
}
*/
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void update_blob()
{
try
{
soci::session sql("postgresql",
"dbname=postgres user=postgres password=postgres");
std::string sql_str(
"update tb_bytea_test set bytea_attr = :byte, base64_text=:t where ID=1");
char attr[20] = {0};
attr[19] = 'D';
string tmp(attr);
string base64_str("AQA3");
std::cout << "tmp.size " << tmp.size() << std::endl;
sql << sql_str, soci::use(tmp), soci::use(base64_str);
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cout << "parameter error" << std::endl;
std::cout << "C create table" << std::endl;
std::cout << "I insert table" << std::endl;
std::cout << "S select table" << std::endl;
std::cout << "D drop table" << std::endl;
std::cout << "SB select blob" << std::endl;
std::cout << "UB select blob" << std::endl;
return 1;
}
std::string cmd(argv[1]);
if (cmd == "C")
{
create_table();
}
if (cmd == "I")
{
insert_table();
}
if (cmd == "S")
{
select_table();
}
if (cmd == "D")
{
drop_table();
}
if (cmd == "SB")
{
select_blob();
}
if (cmd == "UB")
{
update_blob();
}
return 0;
}
At compile time , Use C++11 include The file should contain .lib The connection path should be configured correctly , The required libraries are libsoci-postgresql,libsoci-core
During operation , Need configuration LD_LIBRARY_PATH The path of , hold libsoci-postgresql,libsoci-core And so on .
Connect sqlite Test of
//============================================================================
// Name : soci_sqlite3.cpp
/*
g++ -std=c++0x -I/home/baby/tools/soci-4.0.2/include/soci -I/home/baby/tools/soci-4.0.2/include -I/home/baby/tools/soci-4.0.2/build/include -g -L/home/baby/tools/soci-4.0.2/build/lib -lsoci_sqlite3 -lsoci_core -o soci_sqlite3 soci_sqlite3.cpp
*/
//============================================================================
#include <soci.h>
#include <iostream>
#include <string>
using namespace soci;
using std::string;
void create_table()
{
try
{
soci::session sql("sqlite3", "db=soci_sqlite3.db");
sql << "create table Person(id integer, name text)";
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void drop_table()
{
try
{
soci::session sql("sqlite3", "db=soci_sqlite3.db");
sql << "drop table person";
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void insert_table()
{
try
{
soci::session sql("sqlite3", "db=soci_sqlite3.db");
int id(100);
string name("Bjarne");
sql << "insert into Person values (:ID, :NAME)", use(id), use(name);
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
void select_table()
{
try
{
soci::session sql("sqlite3", "db=soci_sqlite3.db");
int id2;
string name2;
sql << "select id, name from Person", into(id2), into(name2);
std::cout << name2 << " has id " << id2 << std::endl;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cout << "parameter error" << std::endl;
std::cout << "C create table" << std::endl;
std::cout << "I insert table" << std::endl;
std::cout << "S select table" << std::endl;
std::cout << "D drop table" << std::endl;
return 1;
}
std::string cmd(argv[1]);
if (cmd == "C")
{
create_table();
}
if (cmd == "I")
{
insert_table();
}
if (cmd == "S")
{
select_table();
}
if (cmd == "D")
{
drop_table();
}
return 0;
}
边栏推荐
- C language pthread_ cleanup_ Push() and pthread_ cleanup_ Pop() function (used for the resource cleaning task after the termination action in the critical resource program segment to avoid deadlock. T
- jjwt 生成token
- 整体二分?
- (2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
- UiAutomator常用类之UI手势动作
- LogCat工具
- Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较
- js中的数组方法与循环
- Pytorch notes: td3
- (2022杭电多校三)1009.Package Delivery(贪心)
猜你喜欢

零号培训平台课程-1、SQL注入基础

Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)

杂谈:高考

Drools (5): drools advanced syntax

Use reflection to dynamically modify annotation attributes of @excel

(2022 Hangdian multi school III) 1009.package delivery (greedy)
![[wsl2] configure the USB camera connecting the USB device and using the host](/img/03/7ebc7eebeda1598c8f4fdd1e9188c7.png)
[wsl2] configure the USB camera connecting the USB device and using the host

(2022 Niuke multi school III) j-journey (Dijkstra)

How to submit C4d animation to cloud rendering farm for fast rendering?

35. Search Insert Position 搜索插入位置
随机推荐
零号培训平台课程-2、SSRF基础
杂谈:高考
(posted) comparison of Eureka, consumer and Nacos 2
C# Winfrom 常用功能整合-2
查看服务器重启前的 dmesg 日志
(2022杭电多校三)1009.Package Delivery(贪心)
sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
2022 0726 Gu Yujia's study notes
Use Popen to execute a command and get the return result
Turn off the auto start function of Oracle service in centos7
Linear table -- stack and queue
用户解锁SM04 SM12
The error of QT connecting SQLite database and its modification
(posted) comparison of Eureka, consumer and Nacos 1
Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)
(2022牛客多校三)J-Journey(dijkstra)
Oracle cleans up the Database disk space of tables with referenced partitions
Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
在kettle使用循环来处理表中的数据
网络入门——vlan及trunk概述