隨著Linux操作系統(tǒng)的不斷完善與發(fā)展,出現(xiàn)了大量基于 Linux平臺的應用開發(fā),原有的基于UNIX平臺的商業(yè)軟件也不斷被移植到Linux上來。最典型的,Oracle公司宣布,他的現(xiàn)有的及未來所有的
數(shù)據(jù)庫產(chǎn)品和商業(yè)應用都將支持Linux平臺。本文所述OCI for Linux的C語言庫,正是Linux平臺上Oracle的C語言接口。
我們知道,在一個復雜的Oracle
數(shù)據(jù)庫應用中,C程序代碼由于其語言本身的靈活性、高效性,往往被加入到其商務邏輯的核心層模塊中。Oracle
數(shù)據(jù)庫對C語言的接口就是OCI, Oracle 8.05int sqlo_init(int threaded_mode) 初始化程序庫接口,讀出環(huán)境變量,設置相應的全局變量。當前,threaded_mode設為0。
2)int sqlo_connect(int * dbh, char * connect_str) 連接
數(shù)據(jù)庫,dbh為
數(shù)據(jù)庫連接描述符,connect_str為用戶名/口令字符串。
3)int sqlo_finish(int dbh) 斷開
數(shù)據(jù)庫連接。
4)int sqlo_open(int dbh, char * stmt, int argc, char *argv[]) 打開由stmt確定的查詢語句所返回的游標。Argc,argv為查詢的參數(shù),后面我們將用更清晰的方法傳遞參數(shù)。
5)int sqlo_close(int sth) 關閉由上一個函數(shù)打開的游標。
6)int sqlo_fetch(int sth) 從打開的游標中獲取一條記錄,并將之存入一個已分配內存空間中。
7)const char **sqlo_values(int sth, int *numbalues, int dostrip) 從內存中返回上一次sqlo_fetch取得的值,是以字符串形式返回的。
8)以下介紹另一種檢索方式,int sqlo_prepare(int dbh, char const *stmt),返回一個打開的游標sth。
9)int sqlo_bind_by_name(int sth, const char * param_name, int param_type, const void * param_addr, unsigned int param_size, short * ind_arr, int is_array) 將查詢語句的傳入?yún)?shù),按照名字的形式與函數(shù)中的變量綁定。如果你使用數(shù)組,那么參數(shù)param_addr和ind_arr必須指向該數(shù)組。
int sqlo_bind_by_pos(int sth, int param_pos, int param_type, const void * param_addr, unsigned int param_size, short * ind_arr, int is_array) 將查詢語句的傳出值,按照位置順序與函數(shù)中的變量綁定。
10)int sqlo_execute(int sth, int iterations) 執(zhí)行查詢語句。“Iterations”可設為“1”。
11)在執(zhí)行完
數(shù)據(jù)庫操作后,我們可用int sqlo_commit (int dbh)提交操作,或用int sqlo_rollback(int dbh)回滾操作。
12)Libsqlora8還有其他一些操作函數(shù),這里就不一一列出了。
下面舉幾個例子說明這些函數(shù)如何使用。
cstr = "ocitest/ocitest"; //用戶名/口令
status = sqlo_init(0);
if (SQLO_SUCCESS != status)
{ printf ("sql_init failed. Exitingn");
exit(1);
}
status = sqlo_connect(&dbh, cstr); // int dbh
以上源代碼,顯示了如何連接
數(shù)據(jù)庫。
/* Select all and display */
char *select_stmt="SELECT cname, clength, colid FROM ocicolu";
if (0>(sd = sqlo_open(dbh, select_stmt, 0, NULL)))
{ printf("sqlo_open failed: %sn", sqlo_geterror(dbh));
return 0;
}
while (0 == sqlo_fetch(sd,1))
{ v = sqlo_values(sd, NULL, 1);
printf("Result: %sn",v);
}
if (0 > sqlo_close(sd))
{ printf("sqlo_open failed: %sn", sqlo_geterror(dbh));
return 0;
}
以上例子展示了第一種查詢方法,顯然,這種方法較簡單,但不夠靈活。
char *update_stmt =
"UPDATE ocitest.upload_log SET upload_fresh = where log_name = :1";
if (0 <= (sth = sqlo_prepare(dbh, update_stmt)))
{ if (SQLO_SUCCESS !=
(sqlo_bind_by_name(sth, ":1", SQLOT_STR, packet_name, 64, NULL, 0)
))
{ printf("sqlo_bind_param failed failed: %sn", sqlo_geterror(dbh) );
return 0;
}
}
if (SQLO_SUCCESS != sqlo_execute(sth, 1))
{ printf("sqlo_execute failed: %sn", sqlo_geterror(dbh) );
return 0;
}
上面的代碼顯示了如何通過名字綁定變量,“:1”在Oracle SQL語句中表示為一個變量(名字隨意),在sqlo_bind_by_name函數(shù)中與packet_name變量綁定。在變量綁定完畢后,就可以調用sqlo_execute函數(shù)來執(zhí)行這個SQL語句。
好了,我們已經(jīng)向大家介紹了Libsqlora8的基本使用方法,如果希望了解更多內容,Libsqlora8的程序包中帶有詳細的說明和例子,大家不妨自己鉆研一下。有什么心得,歡迎和我聯(lián)系。E-mail:nick_chen@yeah.net /*-------------------------------------------------------------------------
* testlora.c
* Test programm for libsqlora8(Kai Poitschke)
* Assuming you installed the library with prefix=/usr/local, the command
* to compile this is:
* gcc -o sample sample.c -lsqlora8 -L$ORACLE_HOME/lib -lclntsh
*-----------------------------------------------------------------------*/
#include
#include
#include #include "sqlora.h" #define MAX_ITERS 10 #define MAX_LOOPS 1 /* how many time we run the tests */ #define CLOSE_CURSOR 1 /*-------------------------------------------------------------------------
* create our test table
*-----------------------------------------------------------------------*/
int create_table( int dbh )
{
int nkey;
char ckey;
double nv