OTL是一个轻量级的数据库操作工具;可以访问多种数据库(oracle,db2,mysql, sql server等等)
但是它只能用于c++。
otl_connect db ; //数据库连接对象。
otl_stream; //类似于io流操作。
#include <iostream>
#define OTL_ODBC_MYSQL //使用mysql数据库
#include <otlv4.h> //otl的头文件
using namespace std;
otl_connect db;
void insert()
{
otl_stream o(1, // buffer size
"insert into test_tab values(:f1<int>,:f2<char[31]>)",
db // connect object
);
char tmp[32];
for(int i=1;i<=100;++i)
{
sprintf(tmp,"Name%d",i);
o<<i<<tmp;
}
}
void select(const int af1)
{
char stmbuf[1024];
sprintf(stmbuf,
"select * from test_tab where f1>=%d and f1<=%d*2",
af1,
af1
);
otl_stream i(50, // buffer size may be > 1
stmbuf, // SELECT statement
db // connect object
);
int f1;
char f2[31];
while(!i.eof())
{
i>>f1;
cout<<"f1="<<f1<<", f2=";
i>>f2;
if(i.is_null())
cout<<"NULL";
else
cout<<f2;
cout<<endl;
}
}int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
try
{
db.rlogon("root/123@test");
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 int, f2 varchar(30))"
); // create table
insert(); // insert records into the table
select(8); // select records from the 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
}
db.logoff(); // disconnect from ODBC
return 0;
}
本文简要介绍了OTL(Object-Transaction Library)工具,这是一款轻量级的C++数据库操作库,支持包括Oracle、DB2、MySQL、SQL Server等多种数据库。OTL通过otl_connect和otl_stream对象提供类似IO流的操作方式,方便开发者进行数据库交互。

1546

被折叠的 条评论
为什么被折叠?



