以文本方式查看主題 - 曙海教育集團(tuán)論壇 (http://www.022-oo.cn/bbs/index.asp) -- Linux應(yīng)用開發(fā) (http://www.022-oo.cn/bbs/list.asp?boardid=32) ---- Oracle應(yīng)用Linux開發(fā)C (http://www.022-oo.cn/bbs/dispbbs.asp?boardid=32&id=1673) |
-- 作者:wangxinxin -- 發(fā)布時(shí)間:2010-11-23 11:05:05 -- Oracle應(yīng)用Linux開發(fā)C 隨著Linux操作系統(tǒng)的不斷完善與發(fā)展,出現(xiàn)了大量基于 Linux平臺(tái)的應(yīng)用開發(fā),原有的基于UNIX平臺(tái)的商業(yè)軟件也不斷被移植到Linux上來(lái)。最典型的,Oracle公司宣布,他的現(xiàn)有的及未來(lái)所有的數(shù)據(jù)庫(kù)產(chǎn)品和商業(yè)應(yīng)用都將支持Linux平臺(tái)。本文所述OCI for Linux的C語(yǔ)言庫(kù),正是Linux平臺(tái)上Oracle的C語(yǔ)言接口。 我們知道,在一個(gè)復(fù)雜的Oracle數(shù)據(jù)庫(kù)應(yīng)用中,C程序代碼由于其語(yǔ)言本身的靈活性、高效性,往往被加入到其商務(wù)邏輯的核心層模塊中。Oracle數(shù)據(jù)庫(kù)對(duì)C語(yǔ)言的接口就是OCI, Oracle 8.05int sqlo_init(int threaded_mode) 初始化程序庫(kù)接口,讀出環(huán)境變量,設(shè)置相應(yīng)的全局變量。當(dāng)前,threaded_mode設(shè)為0。 2)int sqlo_connect(int * dbh, char * connect_str) 連接數(shù)據(jù)庫(kù),dbh為數(shù)據(jù)庫(kù)連接描述符,connect_str為用戶名/口令字符串。 3)int sqlo_finish(int dbh) 斷開數(shù)據(jù)庫(kù)連接。 4)int sqlo_open(int dbh, char * stmt, int argc, char *argv[]) 打開由stmt確定的查詢語(yǔ)句所返回的游標(biāo)。Argc,argv為查詢的參數(shù),后面我們將用更清晰的方法傳遞參數(shù)。 5)int sqlo_close(int sth) 關(guān)閉由上一個(gè)函數(shù)打開的游標(biāo)。 6)int sqlo_fetch(int sth) 從打開的游標(biāo)中獲取一條記錄,并將之存入一個(gè)已分配內(nèi)存空間中。 7)const char **sqlo_values(int sth, int *numbalues, int dostrip) 從內(nèi)存中返回上一次sqlo_fetch取得的值,是以字符串形式返回的。 8)以下介紹另一種檢索方式,int sqlo_prepare(int dbh, char const *stmt),返回一個(gè)打開的游標(biāo)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ǔ)句的傳入?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) 將查詢語(yǔ)句的傳出值,按照位置順序與函數(shù)中的變量綁定。 10)int sqlo_execute(int sth, int iterations) 執(zhí)行查詢語(yǔ)句!癐terations”可設(shè)為“1”。 11)在執(zhí)行完數(shù)據(jù)庫(kù)操作后,我們可用int sqlo_commit (int dbh)提交操作,或用int sqlo_rollback(int dbh)回滾操作。 12)Libsqlora8還有其他一些操作函數(shù),這里就不一一列出了。 下面舉幾個(gè)例子說(shuō)明這些函數(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ù)庫(kù)。 /* 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; } 以上例子展示了第一種查詢方法,顯然,這種方法較簡(jiǎn)單,但不夠靈活。 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; } 上面的代碼顯示了如何通過(guò)名字綁定變量,“:1”在Oracle SQL語(yǔ)句中表示為一個(gè)變量(名字隨意),在sqlo_bind_by_name函數(shù)中與packet_name變量綁定。在變量綁定完畢后,就可以調(diào)用sqlo_execute函數(shù)來(lái)執(zhí)行這個(gè)SQL語(yǔ)句。 好了,我們已經(jīng)向大家介紹了Libsqlora8的基本使用方法,如果希望了解更多內(nèi)容,Libsqlora8的程序包中帶有詳細(xì)的說(shuō)明和例子,大家不妨自己鉆研一下。有什么心得,歡迎和我聯(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 * create our test table *-----------------------------------------------------------------------*/ int create_table( int dbh ) { int nkey; char ckey; double nv |