当前位置: 首页 > 工具软件 > OCILIB > 使用案例 >

ocilib 使用_ocilib库踩坑记

仲孙翔飞
2023-12-01

1.oracle账户与下载

本文下载了12.2.0.1.0版本的basic\devel\sqlplus包。

2.oci环境搭建(ubuntu虚拟机中搭建oci开发环境)

执行dpkg -i *.deb安装oci环境,

inclue文件位于:/usr/include/oracle/12.2/client64

ORACLE_HOME位于:/usr/lib/oracle/12.2/client64

3.ocilib编译安装(ubuntu虚拟机中编译ocilib)

./configure --with-oracle-lib-path=/usr/lib/oracle/12.2/client64/lib --with-oracle-headers-path=/usr/include/oracle/12.2/client64

make

sudo make install

4.ocilib测试(待定至最后一步)

#include "ocilib.h"

void err_handler(OCI_Error *err){

printf("%s\n", OCI_ErrorGetString(err));

}

int main(int argc, char *argv[])

{

OCI_Connection*cn;

OCI_Statement*st;

OCI_Resultset*rs;

// init with err_handler

OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT);

// connect

cn= OCI_ConnectionCreate("172.17.216.38:1521/rmbtbssvc", "MD", "MD", OCI_SESSION_DEFAULT); // version

printf("Server major version : %i\n", OCI_GetServerMajorVersion(cn));

printf("Server minor version : %i\n", OCI_GetServerMinorVersion(cn));

printf("Server revision version : %i\n\n", OCI_GetServerRevisionVersion(cn));

printf("Connection version : %i\n\n", OCI_GetVersionConnection(cn));

// select

st = OCI_StatementCreate(cn);

OCI_ExecuteStmt(st,"select intcol, strcol from table");

rs=OCI_GetResultset(st);while(OCI_FetchNext(rs))

{

printf("%i - %s\n", OCI_GetInt(rs, 1), OCI_GetString(rs, 2));

}

// clean up

OCI_Cleanup();returnEXIT_SUCCESS;

}

编译:

gcc -o main ocilib-main.c -I/usr/include/oracle/12.2/client64 -L/usr/lib/oracle/12.2/client64/lib -lclntsh -locilib

5.在虚拟机centos中,借助docker安装oracle

https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce-1

启动docker,sudo systemctl start docker

测试docker,sudo docker run hello-world

https://my.oschina.net/u/3446722/blog/983745

https://my.oschina.net/Cleverdada/blog/801124

(1)下载oracle

docker pull wnameless/oracle-xe-11g(docker pull sath89/oracle-12c)

(2)查看镜像

docker images

(3)开启oracle(返回唯一标识符)

sudo docker run -d -v /home/centos/tmp/oracle:/data/oracle_data -p 8080:8080 -p 1521:1521 -e ORACLE_ALLOW_REMOTE=true wnameless/oracle-xe-11g

docker run :创建一个新的容器并运行一个命令。-d: 后台运行容器,并返回容器ID;-p:主机与容器的端口映射(主机ip:容器ip);-v:主机与容器的目录映射。

(4)查看docker运行线程(查询出pid值,类hash的值)

docker ps

(5)结束docker进程(container id作为参数)

docker stop 15dec6104665

(6)查看日志(增加唯一标识符)

docker logs -f 08945b36818d80a9e466b96bf2a1d918c1d7fa9b30dacb9f6fa0f1abcc87d00d

(7)进入刚刚创建的container中

docker exec -it 08945b36818d /bin/bash

在此container中连接oracle(即可进入SQL提示符字面)

su oracle

$ORACLE_HOME/bin/sqlplus / as sysdba

执行SQL语句

SQL>conn system/oracle as sysdba;

SQL>create table t (id number);

SQL>insert into t values (100);

SQL>select * from t;

(8)安装的oracle的默认参数

hostname: localhost

port: 1521

sid: xe

service name: xe.oracle.docker

username: system

password: oracle

6.在虚拟机ubuntu中,借助sqlplus远程连接centos的oracle

https://blog.csdn.net/zhangzl1012/article/details/50814545

(1)首先定位到centos下docker内的oracle,并查看远程数据库的ip、port、username、password,以及service_name。

sqlplus / as sysdba;

SQL>show parameter service_name;

(2)定位到ubuntu中(预先装好client端oracle),执行远程连接,如果测试成功则可进一步使用plsql了。

cd $ORACLE_HOME/bin

./sqlplus system/oracle@192.168.89.131:1521/XE

7.plsql远程连接oracle

(1)下载instantclient_11_2.zip并解压

(2)进入C:\instantclient_11_2,新建network/admin/tnsnames.ora

CENTOS-ORACLE =

(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.89.131)(PORT = 1521))

)

(CONNECT_DATA =

(SERVICE_NAME = XE)

)

)

(3)下载安装plsql,输入用户名system、密码oracle,数据库选择CENTOS-ORACLE,连接选择Normal

(4)进入,开启SQL window,执行建表和插入数据

create table ocilib_table( id number, name varchar2(100));

insert into ocilib_table values (20, 'twenty');

insert into ocilib_table values (30, 'thirty');

insert into ocilib_table values (50, '五十');

select * from ocilib_table;

drop table ocilib_table;

(5)回到ubuntu开发环境,重新修改ocilib示例代码(展示部分行)

cn = OCI_ConnectionCreate("192.168.89.131:1521/XE", "system", "oracle", OCI_SESSION_DEFAULT);

OCI_ExecuteStmt(st, "select id, name from ocilib_table");

8.编写复杂的ocilib逻辑代码

Enjoy it!

 类似资料: