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
實質性的內容了。