当前位置:网站首页>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;
}
边栏推荐
- 在kettle使用循环来处理表中的数据
- Using docker in MAC to build Oracle database server
- Cadence(十一)丝印调整和后续事项
- Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.
- 用oracle来演示外键的使用
- 在rhel8上使用soci连接oracle和postgresql和sqlite
- 软件测试十大必问面试题(附答案和解析)
- Array method and loop in JS
- 用shell来计算文本中的数字之和
- Linear table -- stack and queue
猜你喜欢

Which C4d cloud rendering platform to cooperate with?

杂谈:跟女儿聊为啥要学好文化课

Drools (5): drools basic syntax (3)

2022 0726 Gu Yujia's study notes

(2022 Hangdian multi school III) 1011.taxi (Manhattan maximum + 2 points)

Generics -- learn it, and there are many benefits

35. Search Insert Position 搜索插入位置

Advanced IO outline

VLAN trunk experiment

Li Mu hands-on learning, in-depth learning, V2 transformer and code implementation
随机推荐
SQLite 常用功能整合
Pytorch notes: td3
C# 常用功能整合-3
Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.
? Experiment 7 implementation of PHP management system based on MySQL
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
用户解锁SM04 SM12
Compiling and using log4cxx in rhel7.3
(2022牛客多校三)J-Journey(dijkstra)
The solution of using sqlplus to display Chinese as garbled code
Sort increment with typescript
【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头
tigervnc的使用
用shell来计算文本中的数字之和
【QT】capture.obj:-1: error: LNK2019: 无法解析的外部符号 __imp_htons(解决方法)
Logcat tool
Overall dichotomy?
【golang学习笔记2.1】 golang中的数组中的排序和查找
jjwt 生成token
Excuse me, MySQL timestamp (6) using flick SQL is null. Is there a way to deal with this