otlv4简介

本文简要介绍了OTL(Object-Transaction Library)工具,这是一款轻量级的C++数据库操作库,支持包括Oracle、DB2、MySQL、SQL Server等多种数据库。OTL通过otl_connect和otl_stream对象提供类似IO流的操作方式,方便开发者进行数据库交互。

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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值