Otl是一个C++操控关系数据库的模板库,几乎支持所有主流数据库。OTL中直接操作Oracle主要是通过Oracle提供的OCI接口进行,进行操作DB2数据库则是通过CLI接口来进行,至于MS的数据库和其它一些数据库,则OTL只提供了ODBC来操作的方式。
优点:
a. 跨平台
b. 运行效率高,与C语言直接调用API相当
c. 开发效率高
d. 部署容易,不需要ADO组件,不需要.net framework 等
Otl库只有一个头文件,在使用时包含头文件并定义相应的宏即可。在编译OTL的程序时,需要使用到相应的数据库API,这就要程序在编译时联接lib库文件,不同的数据库对应的lib文件所在位置各不相同。
比如使用odbc:
#define OTL_ODBC // 编译 OTL 4.0/ODBC
// #define OTL_ODBC_UNIX // 如果在Unix下使用UnixODBC,则需要这个宏
#include "otlv4.h" // 包含 OTL 4.0 头文件
比如oracle:
#include <stdio.h>
//#define OTL_ORA10G
#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#define OTL_ORA_UTF8 // Enable UTF8 OTL for OCI9i
#include <otlv4.h> // include the OTL 4.0 header file
#pragma comment(lib, "oci.lib")
Otl接口介绍
主要包括otl_connect,otl_stream,otl_cursor,otl_exception等。
otl_connect db; // 连接对象
otl_connect::otl_initialize(); // 初始化 ODBC 环境
try{
db.rlogon("UID=scott;PWD=tiger;DSN=my_db"); // 连接到 ODBC
otl_cursor::direct_exec
(
db,
"drop table TestTable",
otl_exception::disabled // disable OTL exceptions
); // drop table
//这里完成表的创建
otl_cursor::direct_exec
(
db,
"create table TestTable(ColumA int, ColumB varchar(50),ColumC varchar(50))"
); // create 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.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.var_info<<endl; // print out the variable that caused the error
}
long rpc=otl_cursor::direct_exec(db,"delete from TestTable where ColumA<10 or ColumA>90");
// rpc是作用效果的返回值,otl_cursor::direct_exec为直接执行sql语句
cout<<"Rows deleted: "<<rpc<<endl;
otl_stream
o(1, // 流的缓冲值必须设置为1
"insert into TestTable values(:f1<int>,:f2<char[50]>,:f3<char[50]>)",
// SQL 语句
db // 连接对象
);
char tmp1[32];
char tmp2[30];
for(int i=0;i<100;++i)
{
sprintf(tmp1,"Test Data %d",i);
sprintf(tmp2,"");
o<<i<<tmp1<<tmp2;
}