以文本方式查看主題 - 曙海教育集團論壇 (http://www.hufushizhe.com/bbs/index.asp) -- Linux應用開發 (http://www.hufushizhe.com/bbs/list.asp?boardid=32) ---- Oracle應用Linux開發C (http://www.hufushizhe.com/bbs/dispbbs.asp?boardid=32&id=1673) |
-- 作者:wangxinxin -- 發布時間:2010-11-23 11:05:05 -- Oracle應用Linux開發C 隨著Linux操作系統的不斷完善與發展,出現了大量基于 Linux平臺的應用開發,原有的基于UNIX平臺的商業軟件也不斷被移植到Linux上來。最典型的,Oracle公司宣布,他的現有的及未來所有的數據庫產品和商業應用都將支持Linux平臺。本文所述OCI for Linux的C語言庫,正是Linux平臺上Oracle的C語言接口。 我們知道,在一個復雜的Oracle數據庫應用中,C程序代碼由于其語言本身的靈活性、高效性,往往被加入到其商務邏輯的核心層模塊中。Oracle數據庫對C語言的接口就是OCI, Oracle 8.05int sqlo_init(int threaded_mode) 初始化程序庫接口,讀出環境變量,設置相應的全局變量。當前,threaded_mode設為0。 2)int sqlo_connect(int * dbh, char * connect_str) 連接數據庫,dbh為數據庫連接描述符,connect_str為用戶名/口令字符串。 3)int sqlo_finish(int dbh) 斷開數據庫連接。 4)int sqlo_open(int dbh, char * stmt, int argc, char *argv[]) 打開由stmt確定的查詢語句所返回的游標。Argc,argv為查詢的參數,后面我們將用更清晰的方法傳遞參數。 5)int sqlo_close(int sth) 關閉由上一個函數打開的游標。 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) 將查詢語句的傳入參數,按照名字的形式與函數中的變量綁定。如果你使用數組,那么參數param_addr和ind_arr必須指向該數組。 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) 將查詢語句的傳出值,按照位置順序與函數中的變量綁定。 10)int sqlo_execute(int sth, int iterations) 執行查詢語句。“Iterations”可設為“1”。 11)在執行完數據庫操作后,我們可用int sqlo_commit (int dbh)提交操作,或用int sqlo_rollback(int dbh)回滾操作。 12)Libsqlora8還有其他一些操作函數,這里就不一一列出了。 下面舉幾個例子說明這些函數如何使用。 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 以上源代碼,顯示了如何連接數據庫。 /* 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函數中與packet_name變量綁定。在變量綁定完畢后,就可以調用sqlo_execute函數來執行這個SQL語句。 好了,我們已經向大家介紹了Libsqlora8的基本使用方法,如果希望了解更多內容,Libsqlora8的程序包中帶有詳細的說明和例子,大家不妨自己鉆研一下。有什么心得,歡迎和我聯系。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 |