你好,游客 登录 注册 搜索
背景:
阅读新闻

OTL的使用

[日期:2014-03-17] 来源:Linux社区  作者:xcl168 [字体:大 中 小]

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.

linux
相关资讯       OTL 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款