OTL简单介绍

1.简介
OTL是Oracle 和 ODBC 的模板库,它屏蔽数据库操作的底层,提供数据库连接功能,
以标准 C++ 流的方式完成嵌入式 SQL 语句、存储过程的执行和光标操作。

操作Oracle:OCI接口
操作DB2:CLI接口
其它:ODBC

最新版4.0 地址:http://otl.sourceforge.net/

2.常用类
otl_connect类
功能:建立连接,断开连接,事务提交,事务回滚等。
主要函数:

  • int connected :判断是否已连接。
  • static int otl_initialize(const int threaded_mode=0)
    :初始化OTL数据库环境,先初始化环境再调用连接,多线程访问threaded_mode设为1
  • void set_max_long_size(const int amax_size): 设置缓冲区大小
  • void rlogon(const char* connect_str,const int auto_commit=0):建立连接
    参数:connect_str:数据库连接配置字符串 表现形式如下: OTL4.0/OCIx:USER/PASSWORD(本地数据库)
    USER/PASSWORD@TNS_ALIAS(远程数据库) OTL4.0/ODBC和OTL4.0/DB2_CLI:
    USER/PASSWORD@DSN DSN=value;UID=value;PWD=value
  • auto_commit:数据库事务提交模式,1自动提交,0非自动提交
  • void logoff():断开连接
  • server_attach():服务器连接 OTL/OCI8 only
  • server_detach():服务器分离 OTL/OCI8 only
  • void session_end(): 回话结束 OTL/OCI8 only
  • void session_reopen(const int auto_commit=0):重新打开由session_end关闭的回话
    OTL/OCI8 only
  • void commit():提交数据库事务
  • void rollback():回滚数据库事务
  • otl_exception类 处理OTL操作数据时抛出的异常 unsigned char msg[1000]:保存存异常的具体错误信息。
    unsigned char sqlstate[1000]:打印SQL状态信息 char
    stm_text[2048]:保存导致发生异常错误的SQL语句。 char
    var_info[256]:保存导致发生异常错误的输入/输出变量。
  • otl_stream类 流的具体表现形式,任何通过输入/输出参数使用SQL语句,PL/SQL 块
    constchar* sqlstm, //sqlstm 表示要执行的 SQL 语句
    otl_connect& db, //db 表示使用的数据库连接
    const char* ref_cur_placeholder=0) //ref_cur_placeholder 表示返回的光标(如果有的话)的名字 )
  • void close():关闭流
  • int good(): 测试流是否打开
  • int eof():是否到了流的结尾,和标准 C++ 流的 eof 相同
  • void flush():刷新 otl_stream
    的缓冲区(获取新的数据或者把数据写到数据库中);当缓冲区满的时候,otl_stream 会自动 flush;如果设置了自动提交,则在
    flush 的时候将会触发提交
  • void clean(const int clean_up_error_flag=0) 清空缓冲区而不执行刷新操作
  • int is_null() 测试通过 >> 运算符获取的值是否为空
  • long get_rpc() 获取处理了的记录数目,限于INSERT、DELETE、UPDATE语句
  • otl_column_desc* describe_select(int& desc_len);
    获取字段描述信息,适用于SELECT语句、引用光标(OCI)和存储过程(ODBC for MS SQL Server and
    Sybase)
    class otl_column_desc{ public: char name[512]; // 列名 int dbtype; // 数据库相关的列的数据类型的代码。 int otl_var_dbtype; // OTL defined,
    column datatype code int dbsize; // column length int scale; //
    for numeric columns, column scale int prec; // for numeric
    columns, column precision int nullok; // indicator whether column
    is nullable or not };
  • void set_commit(int auto_commit=0)
    刷新(flush)缓冲区的时候,是否自动提交事务。从更易于理解的角度,建议在显式提交的应用中用 otl_nocommit_stream。

使用方法:

  • 先宏定义需要连接的数据库

例:

#define OTL_ODBC //使用ODBC方式 连接MySQL
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
  • 初始化:otl_connect::otl_initialize();

  • 连接:m_otl_connect.rlogon(DSN=AliVehicle)

  • 执行SQL语句

    otl_cursor::direct_exec 完成表的删除,创建,编辑,查询工作
    使用otl_stream 采用流的方式对表的数据进行操作

实例(转):

#include
#include

#define OTL_ORA8 // Compile OTL 3.1/OCI8
#include // include the OTL 3.1 header file

otl_connect db; // connect object

void insert()// insert rows into table
{
    otl_stream o(50, "insert into test_tab values(:f1,:f2)",db);

    char tmp[32];
    for(int i=1;i<=100;++i)
    {
        sprintf(tmp,"Name%d",i);
        o<<(float)i<<tmp;
    }
}

void select()
{
    otl_stream i(50, "select * from test_tab where f1>=:f and f1<=:f*2",db );// create select stream

    float f1;
    char f2[31];

    i<<8; 
    while(!i.eof())// while not end-of-data
    { 
        i>>f1>>f2;
        cout<<"f1="<<f1<<", f2="<<f2<<endl;
    }

    i<<4; 
    while(!i.eof())// while not end-of-data
    { 
        i>>f1>>f2;
        cout<<"f1="<<f1<<", f2="<<f2<<endl;
    }
}

int main()
{
    otl_connect::otl_initialize(); // initialize OCI environment
    try
    {
        db.rlogon("scott/tiger"); // connect to Oracle
        otl_cursor::direct_exec(db,"drop table test_tab",otl_exception::disabled ); // drop table
        otl_cursor::direct_exec(db,"create table test_tab(f1 number, f2 varchar2(30))");  // create table
        insert(); // insert records into table
        select(); // select records from table
    }
    catch(otl_exception& p)// intercept OTL exceptions
    { 
        cerr<<p.msg<<endl; // print out error message
        cerr<<p.stm_text<<endl; // print out SQL that caused the error
        cerr<<p.var_info<<endl; // print out the variable that caused the error
    }

    db.logoff(); // disconnect from Oracle
    return 0;
}

Output

f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name10
f1=11, f2=Name11
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16
f1=4, f2=Name4
f1=5, f2=Name5
f1=6, f2=Name6
f1=7, f2=Name7
f1=8, f2=Name8

猜你喜欢

转载自blog.csdn.net/weixin_43002236/article/details/81808235