Oracle数据库开发(六).OCI应用初探

11 篇文章 0 订阅
11 篇文章 0 订阅

Oracle Database Development (6). A first exploration of OCI

Vert Melon

Jun 25,2007

1.Preface

 Last time , i provided a full example of OCI , which contains the actual code of connection 
and SQL . Certainly it could not cover all of the knowledge , but i think it is a good beginning 
for the fresher .

 As the same as Pro*C you might confirm that you have installed it correctly .  Look at this 
paths as follows :
 
 Windows   :   $ORACLE_HOME/oci
 Linux/Unix  :   $ORACLE_HOME/rdbms/demo
 
 There are many examples offered by ORACLE . Though it seems like a clutter , you also 
can get some useful imformation .

 The main platform we use here is Linux .  And the Windows ?  How can i config it in Windows ? 
It would be a comfy thing if you have read the chapters recorded in  <Oracle Database Development (1). Config OCI In Windows> .
It is easily comprehend by analogy .


2.Something you should know

 What is the Oracle Call Interface?

 The Oracle Call Interface (OCI) is a set of low-level APIs (Application Programming Interface Calls) 
used to interact with Oracle databases. It allows one to use operations like logon, execute, 
parse, fetch, etc. OCI programs are normally written in C or C++, although they can be written
in almost any programing language. Unlike with the Oracle Precompilers (like Pro*C and Pro*COBOL), 
OCI programs are not precompiled.

 Also I have found a official explaination about the choice between Pro*C and OCI .
 
 Should one use OCI or the Oracle Precompilers?
 
 OCI is superior to Pro*C in the following ways: 
  Performance is much better with OCI 
  Reduced code size 
  Direct access to built-in functions (No intermediate files or substitutions). 
  Piecewise Operation on LONG fields (All LONG field problems are solved) 
  In Pro*C one cannot dynamically allocate memory to be used as bind variables 
  You cannot control the Pro*C precompiler to provide better and more compilable C-code. 
  ...
   
 Common problems with OCI: 
  OCI code is difficult to write and to maintain 
  Very few people can write, let alone maintain OCI code 
  ...
  
 An OCI application program must do the following: 
  Connect to one or more databases: call the OCILogon (olog, olon or orlon) routines 
  Open the cursors needed by the program: use oexec, oexn, ofen or oftech calls. 
  Process the SQL statements that are needed to perform the application's tasks. 
  Close the cursors using the oclose routine. 
  Disconnect from the databases: use ologoff to close an open connection to oracle.  

3.Obsolescent OCI Routines

 After get through the section "Something you should know" ,  you may find some strange 
words : oexec , ologoff  or oclose . There are old routines in preceding release . 
 Release 8.0 of the Oracle Call Interface introduced an entirely new set of functions which 
were not available in release 7.3. Release 8.1 added more new functions. Oracle9i OCI continues 
to support these new functions, and adds more new calls. The earlier 7.x calls are still available, 
but Oracle strongly recommends that existing applications use the new calls to improve performance 
and provide increased functionality.
 To get more information ,  check it in the chapter named "Introduction and Upgrading" in 
OCI document .


4. Introduce to OCI Makefile

 It's time to make out the source file , and the first one is Makefile which is a trunk in a project .
But then it is the end of  a first exploration .

 Notice that the head files of OCI are put in two directories and the lib file is libclntsh.so.9.0 
which is the same as Pro*C .  This is a simple one ,  just copy the code in last article and 
divide into the corresponding files as the list . Then use "make all" or "make clean" to deal with 
the source file automaticly . You would see something like this .

[root@liwei oci]# make clean;
rm -f OCIDB OCIDB.o OCIException.o Exception.o OCIError.o Main.o
[root@liwei oci]# make all;
[OCIDB.o]
---------------------
g++ -g -o OCIDB.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c OCIDB.cpp

[OCIException.o]
---------------------
g++ -g -o OCIException.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c OCIException.cpp

[Exception.o]
---------------------
g++ -g -o Exception.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c Exception.cpp

[OCIError.o]
---------------------
g++ -g -o OCIError.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c OCIError.cpp

[Main.o]
---------------------
g++ -g -o Main.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c Main.cpp

[link] ... 
---------------------
g++ -g -o OCIDB OCIDB.o OCIException.o Exception.o OCIError.o Main.o -L/home/ora/ora9/oracle/lib -lclntsh

 That's good . Take a rest for next step .

-------------------------------------------------------------------------------------------------------

Oracle数据库开发(六).OCI应用初探

草木瓜

2007.6.25  
 
一、序

 在上一次,我提供了一个完整的OCI示例,内容包括数据库连接和SQL操作的实际代码。
当然一个小例子不可能包括所有内容,不过我认为对初学者是一个不错的开始。

 与Pro*C一样,需要确认安装OCI组件,查看下面的路径:
 
 Windows   :   $ORACLE_HOME/oci
 Linux/Unix  :   $ORACLE_HOME/rdbms/demo
 
 这些目录包含一些由ORACLE提供的一些示例。虽然有些杂乱,不过还是一些帮助的。
 我们这里使用的主要平台是Linux。 Windows下怎么设置呢?可以参见《Oracle数据库
开发(一).Windows下配置使用ProC》一文,都是类似的。

 
二、一些你需要知道的东西

 OCI是什么 ?
 
 OCI是一组底层的API(应用程序接口),主要和Oracle数据库进行交互。你可以调用一些
操作如 logon , execute, parse, fecth 等等。OCI支持大数据语言,通常使用C/C++。与Oracle
Pro*C等不同,OCI不需要预编译。

 我这里也找着一份关于在Pro*C和OCI之间选择的官方说明。
 
 我应该使用OCI还是Pro*C?
 
 OCI比Pro*C的一些优势:
  
  OCI的性能十分出色
  代码大量缩减
  对内置函数直接访问
  对LONG类型的分段操作(可以处理LONG相关的任何错误)
  Pro*C不能为绑定变量动态分配内存
  不能控制Pro*C自动生成的代码 
  
 OCI开发的一些常见问题:
 
  OCI代码不容易掌握
  ...
  
 OCI开发流程:
 
  连接多个数据库:使用OCILogon (olog, olon or orlon)
  打开游标:oexec, oexn, ofen 或者 oftech
  执行相应SQL语句
  关于游标:oclose
  断开连接:ologoff
  
三、废弃的一些程序标准

 看过上节,你会发现一些奇怪的单词,oexec , ologoff  或 oclose 。这些都是先前版本
旧的OCI标准。

 OCI 8.0 引入一套全新的程序结构,是7.3以前没有的。8.1版本又扩展了一些函数。
在Oracle 9i中虽然支持这些旧的标准,不过Oracle强烈建议使用全新的OCI标准库。
 参考OCI文档中"介绍和升级内容"一节,获取更多信息。
 
四、OCI Makefile 介绍

 现在差不多该介绍源代码了,首先的是Makefile文件,可以说成是整个项目的中心,参
看例中的文件内容。注意头文件目录有两个,库文件和Pro*C使用的一样,还是 libclntsh.so.9.0 ,
相关路径自已调整。
 这个Makefile比较简单,把前面文章罗列的所有代码复制并建立相应文件,使用"make all"
"make clean" 命令,你应该能看到如下类似的内容: 

[root@liwei oci]# make clean;
rm -f OCIDB OCIDB.o OCIException.o Exception.o OCIError.o Main.o
[root@liwei oci]# make all;
[OCIDB.o]
---------------------
g++ -g -o OCIDB.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c OCIDB.cpp

[OCIException.o]
---------------------
g++ -g -o OCIException.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c OCIException.cpp

[Exception.o]
---------------------
g++ -g -o Exception.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c Exception.cpp

[OCIError.o]
---------------------
g++ -g -o OCIError.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c OCIError.cpp

[Main.o]
---------------------
g++ -g -o Main.o -I/home/ora/ora9/oracle/rdbms/demo -I/home/ora/ora9/oracle/rdbms/public -c Main.cpp

[link] ... 
---------------------
g++ -g -o OCIDB OCIDB.o OCIException.o Exception.o OCIError.o Main.o -L/home/ora/ora9/oracle/lib -lclntsh

 Makefile相关知识可以去Google一下,文章从现在开始,就要陆续介绍一些OCI
实质性的内容了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值