OTL的使用


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.

相关内容