C++类库:OTL通用的数据库连接类库
2013-09-27 23:12
288 查看
OTL是一个纯C++的通用数据库连接模板库,可以支持各种当下流行的数据库,如Oracle,Sybase, MySQL,
PostgreSQL, EnterpriseDB, SQLite, MS ACCESS,
Firebird等等.它是一个跨平台类库,在MS Windows, Linux/Unix/Mac OS X 都可以使用。
下载网址:http://otl.sourceforge.net/
OTL使用简单, 只要头文件中包含有: #include "otlv4.h"
就可,实际上整个OTL就一个.H的文件,使用起来极为的方便。
OTL使用方法:
1、首先指定要连接的数据库类型,OTL用宏定义来指定要连接的数据库类型。OTL会根据这个宏定义来初始化数据库连接的环境。
相关的宏定义列表 http://otl.sourceforge.net/otl3_compile.htm
如: #define OTL_ORA8I 表示连接Oracle 8i 数据库。
2、例子
#include
<iostream>
using namespace std;
#include <stdio.h>
#define OTL_ORA8I // 指定连接的数据库类型
#include <otlv4.h> // include the OTL
4 header file
otl_connect db; // 定义数据库实例
void insert()
// 添加记录
{
otl_stream o(50, // buffer size
"insert into test_tab
values(:f1<float>,:f2<char[31]>)",
// 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<<static_cast<float>(i)<<tmp;
}
}
//查询记录
void select()
{
otl_stream i(50, // buffer size
"begin "
" open :cur1 for "
" select * from test_tab
"
" where
f1>=:f<int> 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<desc_len;++n){
cout<<"==========
COLUMN
#"<<n+1<<"
==========="<<endl;
cout<<"name="<<desc
.name<<endl;
cout<<"dbtype="<<desc
.dbtype<<endl;
cout<<"otl_var_dbtype="<<desc
.otl_var_dbtype<<endl;
cout<<"dbsize="<<desc
.dbsize<<endl;
cout<<"scale="<<desc
.scale<<endl;
cout<<"prec="<<desc
.prec<<endl;
cout<<"nullok="<<desc
.nullok<<endl;
}
}
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<<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();
//断开数据库连接
return 0;
}
PostgreSQL, EnterpriseDB, SQLite, MS ACCESS,
Firebird等等.它是一个跨平台类库,在MS Windows, Linux/Unix/Mac OS X 都可以使用。
下载网址:http://otl.sourceforge.net/
OTL使用简单, 只要头文件中包含有: #include "otlv4.h"
就可,实际上整个OTL就一个.H的文件,使用起来极为的方便。
OTL使用方法:
1、首先指定要连接的数据库类型,OTL用宏定义来指定要连接的数据库类型。OTL会根据这个宏定义来初始化数据库连接的环境。
相关的宏定义列表 http://otl.sourceforge.net/otl3_compile.htm
如: #define OTL_ORA8I 表示连接Oracle 8i 数据库。
2、例子
#include
<iostream>
using namespace std;
#include <stdio.h>
#define OTL_ORA8I // 指定连接的数据库类型
#include <otlv4.h> // include the OTL
4 header file
otl_connect db; // 定义数据库实例
void insert()
// 添加记录
{
otl_stream o(50, // buffer size
"insert into test_tab
values(:f1<float>,:f2<char[31]>)",
// 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<<static_cast<float>(i)<<tmp;
}
}
//查询记录
void select()
{
otl_stream i(50, // buffer size
"begin "
" open :cur1 for "
" select * from test_tab
"
" where
f1>=:f<int> 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<desc_len;++n){
cout<<"==========
COLUMN
#"<<n+1<<"
==========="<<endl;
cout<<"name="<<desc
.name<<endl;
cout<<"dbtype="<<desc
.dbtype<<endl;
cout<<"otl_var_dbtype="<<desc
.otl_var_dbtype<<endl;
cout<<"dbsize="<<desc
.dbsize<<endl;
cout<<"scale="<<desc
.scale<<endl;
cout<<"prec="<<desc
.prec<<endl;
cout<<"nullok="<<desc
.nullok<<endl;
}
}
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<<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();
//断开数据库连接
return 0;
}
相关文章推荐
- C++类库:OTL通用的数据库连接类库
- C#编写的OleDb数据库连接通用类库
- OTL--c++中连接数据库的方法
- C++类库:OTL连接MySQL ODBC数据库(insert, update, select)
- C#-继承IDbConnection连接不同数据库,通用的DbHelper(1)
- 详细的C++连接数据库
- 数据库连接检查以及在c++中捕获mysql报出的错误
- C++连接数据库(ODBC/win32编程)
- C++类库最详细介绍附学习连接地址
- otl 数据库连接
- 笔记 c++ mysql 数据库连接
- asp.net 通用的连接数据库实例代码
- C++连接数据库分布进行
- JAVA--JDBC建立通用数据库连接
- Vim 一键编译、连接、运行 C/C++ 单文件(Windows 与 Linux通用)
- ubuntu c++下连接mysql数据库和数据库的使用
- 通用数据库连接工具介绍
- C++在WINDOWS平台使用ADO连接数据库
- C++连接数据库并实现读写操作
- 用C++或C#连接oracle、postgres数据库的字符串和语句