使用OTL进行数据库编程

操作环境:
1. 操作系统:Windows XP Professional with SP2。
2. 编程环境:Visual C++ 6.0 with SP6。
3. 数据库环境:Access 2003。
OTL简介:
OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库,例如Oracle, MS SQL Server, Sybase, Informix, MySQL, DB2, Interbase / Firebird, PostgreSQL, SQLite, SAP/DB, TimesTen, MS ACCESS等等。OTL中直接操作Oracle主要是通过Oracle提供的OCI接口进行,进行操作DB2数据库则是通过CLI接口来进行,至于MS的数据库和其它一些数据库,则OTL只提供了ODBC来操作的方式。当然Oracle和DB2也可以由OTL间接使用ODBC的方式来进行操纵。
在MS Windows and Unix 平台下,OTL目前支持的数据库版本主要有:Oracle 7 (直接使用 OCI7), Oracle 8 (直接使用 OCI8), Oracle 8i (直接使用OCI8i), Oracle 9i (直接使用OCI9i), Oracle 10g (直接使用OCI10g), DB2 (直接使用DB2 CLI), ODBC 3.x ,ODBC 2.5。OTL最新版本为4.0,参见 http://otl.sourceforge.net/ ,下载地址 http://otl.sourceforge.net/otlv4_h.zip
优点:
      a. 跨平台
      b. 运行效率高,与C语言直接调用API相当
      c. 开发效率高,起码比ADO.net使用起来更简单,更简洁
      d. 部署容易,不需要ADO组件,不需要.net framework 等
缺点:
      a. 说明文档以及范例不足够丰富(暂时性的)
     其实现在它提供有377个使用范例可参考,下载地址: http://otl.sourceforge.net/otl4_examples.zip
建立数据源
1.依次点击“开始->控制面板”,打开“控制面板”界面,双击“管理工具”,然后再双击“数据源(ODBC)”,就打开了“ODBC数据源管理器”,选择“系统DSN”。
2.单击“添加”,弹出“创建新数据源”对话框,选择“Microsoft Access Driver(*.mdb)”。
3.点击“完成”,弹出“ODBC Microsoft Access安装”对话框,单击“创建”,开始创建数据库,弹出“新建数据库”对话框,添加数据库名称my_db和选择数据库存放目录,单击“确定”,创建完成,然后添加数据源名:my_db。点击“确定”。
4.然后在系统数据源中就有我们刚才添加的数据源。
5.单击“确定”,完成数据源的创建。
OTL编程
下面我们用一个实例来说明:
1. 创建数据表:TestTable ( ColumA int , ColumB varchar(50),ColumC varchar(50) )
2. 插入100条数据,ColumA 为数据的 id 范围:0-99 , ColumB=”Test Data %d” , 其中 %d=id 。
3. 删除表中ColumA 中小于10和大于90的数据。
4. 将ColumA为3的倍数的记录中ColumC更新为ColumB的内容。
具体代码为:
None.gif#include <iostream>
None.gif using  namespace std;
None.gif#include <stdio.h>
None.gif#include < string.h>
None.gif#include <stdlib.h>
None.gif #define OTL_ODBC  //  编译 OTL 4.0/ODBC
None.gif //  #define OTL_ODBC_UNIX  //  如果在Unix下使用UnixODBC,则需要这个宏
None.gif
#include "otlv4.h"  //  包含 OTL 4.0 头文件
None.gif
otl_connect db;  //  连接对象
None.gif
None.gif
// 此函数完成插入100条数据,ComulA为数据的id,范围为0-99,
None.gif
// ColumB="Test Data %d",其中%d=id
None.gif
void insert()  
None.gif //  向表中插入行
ExpandedBlockStart.gif

InBlock.gif// 打开一个通用的流,以模板的方式向表中插入多项数据
InBlock.gif
otl_stream
InBlock.gif  o(1, // 流的缓冲值必须设置为1
InBlock.gif
  "insert into TestTable values(:f1<int>,:f2<char[50]>,:f3<char[50]>)", 
InBlock.gif  // SQL 语句
InBlock.gif
  db  // 连接对象
InBlock.gif
  );
InBlock.gifchar tmp1[32];
InBlock.gifchar tmp2[30];
InBlock.gif
ExpandedSubBlockStart.giffor(int i=0;i<100;++i){
InBlock.gif  sprintf(tmp1,"Test Data %d",i);
InBlock.gif  sprintf(tmp2,"");
InBlock.gif  o<<i<<tmp1<<tmp2;
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedBlockEnd.gif}
None.gif // 此函数完成删除表中ColumA中小于10和大于90的数据
None.gif
void delete_rows()
ExpandedBlockStart.gif
InBlock.giflong rpc=otl_cursor::direct_exec(db,"delete from TestTable where ColumA<10 or ColumA>90");
InBlock.gif// rpc是作用效果的返回值,otl_cursor::direct_exec为直接执行sql语句
InBlock.gif
cout<<"Rows deleted: "<<rpc<<endl;
ExpandedBlockEnd.gif}
None.gif
None.gif // 此函数完成将ColumA为3的倍数的记录中ColumC更新为ColumB的内容
None.gif
void update()
None.gif //  更新表
ExpandedBlockStart.gif
{
InBlock.gifotl_stream 
InBlock.gif  o(1, // 缓冲值
InBlock.gif
  "UPDATE TestTable "
InBlock.gif  "   SET ColumC=:f2<char[50]> "
InBlock.gif  " WHERE ColumA=:f1<int>", 
InBlock.gif        // UPDATE 语句
InBlock.gif
  db // 连接对象
InBlock.gif
  );
InBlock.gifotl_stream c(1,"select ColumB from TestTable where ColumA=:f3<int>",db);
InBlock.gifchar temp[10];
InBlock.giffor(int i=10;i<91;i++)
ExpandedSubBlockStart.gif{
InBlock.gif  if(i%3==0)
ExpandedSubBlockStart.gif  {
InBlock.gif   c << i;
InBlock.gif   c >> temp;
InBlock.gif   o << temp << i;
ExpandedSubBlockEnd.gif  }
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedBlockEnd.gif}
None.gif
None.gif int main()
ExpandedBlockStart.gif {
InBlock.gifotl_connect::otl_initialize(); // 初始化 ODBC 环境
ExpandedSubBlockStart.gif
try{
InBlock.gif  
InBlock.gif  db.rlogon("UID=scott;PWD=tiger;DSN=my_db"); // 连接到 ODBC
InBlock.gif  
//或者使用下面的连接语句方式。 
InBlock.gif  
//  db.rlogon("scott/tiger@firebird"); // connect to ODBC, alternative format
InBlock.gif  
// of connect string 
InBlock.gif
  
InBlock.gif  otl_cursor::direct_exec
InBlock.gif   (
InBlock.gif   db,
InBlock.gif   "drop table TestTable",
InBlock.gif   otl_exception::disabled // disable OTL exceptions
InBlock.gif
   ); // drop table
InBlock.gif  
InBlock.gif  
//这里完成表的创建
InBlock.gif
  otl_cursor::direct_exec
InBlock.gif   (
InBlock.gif   db,
InBlock.gif   "create table TestTable(ColumA int, ColumB varchar(50),ColumC varchar(50))"
InBlock.gif   );  // create table
InBlock.gif
  
InBlock.gif  insert(); // insert records into the table
InBlock.gif  
//  update(10); // update records in the table
InBlock.gif
  delete_rows();
InBlock.gif  update();
InBlock.gif  
ExpandedSubBlockEnd.gif}
InBlock.gif
ExpandedSubBlockStart.gifcatch(otl_exception& p){ // intercept OTL exceptions
InBlock.gif
  cerr<<p.msg<<endl; // print out error message
InBlock.gif
  cerr<<p.stm_text<<endl; // print out SQL that caused the error
InBlock.gif
  cerr<<p.sqlstate<<endl; // print out SQLSTATE message
InBlock.gif
  cerr<<p.var_info<<endl; // print out the variable that caused the error
ExpandedSubBlockEnd.gif
}
InBlock.gif
InBlock.gifdb.logoff(); // disconnect from the database
InBlock.gif

InBlock.gifreturn 0;
InBlock.gif
ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值