C 通過OCCI操作Oracle資料庫詳解

2021-06-20 22:33:23 字數 2220 閱讀 6487

1.安裝occi

oracle-instantclient-sqlplus-10.2.0.5-1.i386.rpm

oracle-instantclient-devel-10.2.0.5-1.i386.rpm

oracle-instantclient-odbc-10.2.0.5-1.i386.rpm

oracle-instantclient-basic-10.2.0.5-1.i386.rpm

安裝完成之後,會在/usr/lib下多個oracle 共享庫資料夾,在/usr/include下多個oracle 標頭檔案(介面)資料夾(可以將他們放到環境變數中)。我的資料庫版本是10.2.0,下面以此為例。

2.編寫helloworld程式測試連線

#include #define linuxocci //

避免函式重定義錯誤

#include

using

namespace

std;

using

namespace

oracle::occi;

intmain()

catch

(sqlexception e)

environment::terminateenvironment(env);

cout

<

end!

"<

return0;

}

編譯命令:

g++ test.cc -o test -i/usr/include/oracle/10.2.0.5/client -l/usr/lib/oracle/10.2.0.5/client/lib -locci -lsqlplus

我沒有將occi的路徑加入到環境變數中,所以此處要顯示列出目錄才能通過編譯,找到共享庫。

執行./test會報錯,libocci.so找不到,解決辦法很簡單:將/usr/lib/oracle/.../lib下的庫加入到ld_library_path中就可以了。

輸出結果:

success

conn success

end!

注:這件不幸的事情可能只發生在我身上了,本人系統中有三個使用者,其中乙個是oracle,而程式是用另乙個使用者寫的,於是編譯通過了,但是執行總是報錯:

ora-12162: tns:net service name is incorrectly specified

後來查明,這個是由於沒有設定並匯出oracle_sid。換了oracle使用者試試,居然執行通過了,真的很傷心,原來那個使用者沒有設定oracle環境變臉怎麼能直接本地訪問呢。

3.進行一些操作,執行sql語句

employees.h

/*

* this file contains the employees class declaration

*/#include

#include

#include

using

namespace

oracle::occi;

using

namespace

std;

class

employees ;

employees::employees()

catch (sqlexception&ex)

}employees::~employees()

void

employees::list()

catch (sqlexception&ex)

if(stmt)

catch (sqlexception&ex)

if(rs)

cout

<

stmt->closeresultset(rs);

}con->terminatestatement(stmt);}}

main.cc

#include "

employees.h

"using

namespace

std;

using

namespace

oracle::occi;

int main (void

)

**:

C 使用occi連線oracle資料庫

遇到的問題 occi就是oracle c call inte ce。然後網頁往下拉,這個對應的sdk包就有相關標頭檔案和使用例子等。我把對應標頭檔案和lib庫放到了我的計算機的這裡 1 然後用使用的專案 vs2019下 屬性配置相關的標頭檔案引用和lib庫引用,d usinglibs我配了環境變數l...

使用OCCI連線Linux下Oracle資料庫

occi oracle c call inte ce c 程式與oracle 資料庫實現互動的應用程式介面,它以動態連線庫的形式提供給使用者。occi 對oci 實行了物件級的封裝,其底層仍是 oci occi連線 linux 下的oracle 資料庫 1 安裝 linux 下的oracle 客戶端...

使用Occi連線Oracle資料庫

1 首先應該建立環境變數 environment 類是occi 程式的基礎類,所有的 occi 物件的建立都是依計 environment 物件來建立的,所以 environment 物件的建立必須放在第一位,而且也必須是最後乙個被終止的。例如 首先建立乙個 environment 物件env 然後...