歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OTL的使用

OTL的使用

日期:2017/3/1 9:47:06   编辑:Linux編程

OTL可通過odbc,數據庫本身的連接庫如oci,與數據庫進行交互,跨平台,跨數據庫,api使用方便且僅只是個頭文件,我一直都使用這個。

OTL的官網是:http://otl.sourceforge.net/ 裡面例子文檔什麼的都相當全。

以OTL連接Oracle 11g為例,說明下在VS中的使用方式:

1. VS 編譯環境設置

a. 在工程項目中引入otlv4.h頭文件

b. 在vs中指定頭文件目錄:

C:\oracle\product\11.2.0\dbhome_1\OCI\include

c.指定 附加庫目錄:

C:\oracle\product\11.2.0\dbhome_1\OCI\lib\MSVC

d. 輸入附加庫:

oci.lib

2. 以普通用戶連接Oracle數據庫的例子:

#include <stdio.h>
#include <iostream>

#define OTL_ORA11G_R2 // Compile OTL 4.0/OCI11.2

#define OTL_ORA_UTF8
#include "otlv4.h" // include the OTL 4 header file

//#pragma comment(lib,"oci.lib")

using namespace std;
otl_connect oracledb;

int main(void)
{
//int OTLSession_mode = OCI_SYSDBA;

try{
otl_connect::otl_initialize();
oracledb.rlogon("system/xcldb@xcldb");
//.......

}catch(otl_exception &p)
{
cerr<<p.msg<<endl;
cerr<<p.stm_text<<endl;
cerr<<p.var_info<<endl;
}
oracledb.logoff();
return 0;
}

編譯注意事項:

a. 如果使用的Oracle oci是64位的,vs就要編譯成64位的程序,如果編譯成32位,會提示找不到Oracle的動態庫。

b.因為Oracle連接數據庫較慢,有些會使用多線程,這時要注意線程安全問題.

通過otl_initialize()函數設不同的參數來解決.

// Threaded_mode = 1 means the multi-threaded mode, 0 -- the single threaded mode
otl_connect::otl_initialize(0);

3. 用SYSDBA登錄身份連接Oracle數據庫

當SYS用戶連接Oracle時,如果用普通用戶會報"ORA-28009 應當以sysdba或sysoper建立sys連接"錯誤.

在session_begin中指定用戶登錄身份即可:

db.session_begin(m_strUser.c_str(),m_strPassword.c_str(),0,OCI_SYSDBA);

OCI_DEFAULT
OCI_SYSDBA -- in this mode, the user is authenticated for SYSDBA access.
OCI_SYSOPER -- in this mode, the user is authenticated for SYSOPER access.

Copyright © Linux教程網 All Rights Reserved