c 连接oracle otl,C++类库:OTL通用的数据库连接类库

OTL是一个纯C++的通用数据库连接模板库,可以支持各种当下流行的数据库,如Oracle,Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite, MS ACCESS, Firebird等等.它是一个跨平台类库,在MS Windows, Linux/Unix/Mac OS X 都可以使用。

OTL使用简单, 只要头文件中包含有: #include "otlv4.h" 就可,实际上整个OTL就一个.H的文件,使用起来极为的方便。

OTL使用方法:

1、首先指定要连接的数据库类型,OTL用宏定义来指定要连接的数据库类型。OTL会根据这个宏定义来初始化数据库连接的环境。

如: #define OTL_ORA8I 表示连接Oracle 8i 数据库。

2、例子

#include

using namespace std;

#include

#define OTL_ORA8I // 指定连接的数据库类型

#include // include the OTL 4 header file

otl_connect db; // 定义数据库实例

void insert()

// 添加记录

{

otl_stream o(50, // buffer size

"insert into test_tab values(:f1,:f2)",

// SQL statement

db // connect object

);

char tmp[32];

for(int i=1;i<=100;++i){

#if defined(_MSC_VER)

#if (_MSC_VER >= 1400) // VC++ 8.0 or higher

sprintf_s(tmp,sizeof(tmp),"Name%d",i);

#else

sprintf(tmp,"Name%d",i);

#endif

#else

sprintf(tmp,"Name%d",i);

#endif

o<(i)<

}

}

//查询记录

void select()

{

otl_stream i(50, // buffer size

"begin "

" open :cur1 for "

" select * from test_tab "

" where f1>=:f and f1<=:f*2; "

"end;", // SELECT statement via referenced cursor

db, // connect object

":cur1" // referenced cursor placeholder name

);

// create select stream

otl_column_desc* desc;

int desc_len;

i<<4; // Before the child SELECT statement can be described,

// the PL/SQL master block needs to be executed.

// Forcing the execution of the blcok by entering its input

// patameter.

desc=i.describe_select(desc_len);

for(int n=0;n

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

}

}

int main()

{

otl_connect::otl_initialize(); // 初始化Oracle环境

try{

db.rlogon("scott/tiger"); // 连接Oracle

otl_cursor::direct_exec

(

db,

"drop table test_tab",

otl_exception::disabled // disable OTL exceptions

); // drop table

otl_cursor::direct_exec

(

db,

"create table test_tab(f1 number, f2 varchar2(30))"

); // create table

insert(); select();

}

catch(otl_exception& p){ // intercept OTL exceptions

cerr<

cerr<

cerr<

}

db.logoff(); //断开数据库连接

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值