OTL介绍

转自http://www.cppprog.com/2008/1221/2.html

本文主要介绍了OTL和怎样在C++程序中使用OTL操作数据库。
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
OTL 的使用 :
   OTL 使用起来也很简单,使用不同的数据库连接,主要是根据需要在程序开始的宏定义来指定的。  OTL 是首先根据这个宏定义来初始化数据库连接环境。  OTL 中用来区分连接方式的宏定义主要有下面这些 :
 OTL_ORA7, OTL_ORA8, OTL_ODBC, OTL_DB2_CLI, OTL_ODBC_MYSQL...
不同的宏对应的数据库 API ,具体说明如下:
宏定义名
说明
OTL_DB2_CLI
for DB2 Call Level Interface (CLI)
OTL_INFORMIX_CLI
for Informix Call Level Interface for Unix (when  OTL_ODBC_UNIX is enabled).
OTL_IODBC_BSD
for ODBC on BSD Unix, when iODBC package is used
OTL_ODBC
for ODBC
OTL_ODBC_MYSQL
for MyODBC/MySQL. The difference between OTL_ODBC_MYSQL and OTL_ODBC is that transactional ODBC function calls are turned off for OTL_ODBC_MYSQL, since MySQL does not have transactions
OTL_ODBC_
POSTGRESQL
for the PostgreSQL ODBC driver 3.5 (and higher) that are connected to PostgerSQL 7.4 / 8.0  (and higher)  servers.
OTL_ODBC_UNIX
for ODBC bridges in Unix
OTL_ODBC_zOS
for ODBC on IBM zOS.
OTL_ODBC_XTG_IBASE6
for Interbase 6.x via XTG Systems'  ODBC driver. The reason for introducing this #define is that the ODBC driver is the only Open Source ODBC driver for Interbase. Other drivers, like Easysoft's ODBC for Interbase, are commercial products, and it beats the purpose of using Interbase, as an Open Source.database server.
OTL_ORA7
for OCI7
OTL_ORA8
for OCI8
OTL_ORA8I
for OCI8i
OTL_ORA9I
for OCI9i. All code that compiles and works under #define OTL_ORA7, OTL_ORA8, and OTL_ORA8I, should work when OTL_ORA9I is used
OTL_ORA10G
for OCI10g. All code that compiles and works  under #define OTL_ORA7, OTL_ORA8, OTL_ORA8I, OTL_ORA9I, should work with OTL_ORA10G.
OTL_ORA10G_R2
for OCI10g, Release 2 (Oracle 10.2). All code that compiles and works  under #define OTL_ORA7, OTL_ORA8, OTL_ORA8I, OTL_ORA9I, and OTL_ORA10G should work with OTL_ORA10G_R2 .
我们在编译 OTL 的程序时,需要使用到相应的数据库 API ,这就要程序在编译时联接 lib 库文件,不同的数据库对应的 lib 文件所在位置各不相同,下面是分别在 windows Unix 下的数据库 API 所需要的头文件及 lib 文件所在的位置列表:
API
API header files for Win dows
API libraries for Windows
OCI7
In <ORACLE_HOME>/oci/include
 <ORACLE_HOME>/oci/lib/<compiler_specific>/ociw32.lib
OCI8
In <ORACLE_HOME>/oci/include
 <ORACLE_HOME>/oci/lib/<compiler_specific>/oci.lib
OCI8i
In <ORACLE_HOME>/oci/include
 <ORACLE_HOME>/oci/lib/<compiler_specific>/oci.lib
OCI9i
In <ORACLE_HOME>/oci/include
 <ORACLE_HOME>/oci/lib/<compiler_specific>/oci.lib
OCI10g
In <ORACLE_HOME>/oci/include
 <ORACLE_HOME>/oci/lib/<compiler_specific>/oci.lib
ODBC
Normally, in one of the C++ compiler system directories, no need to include explicitly.
Normally, in one of the C++ compiler system directories: odbc32.lib
DB2 CLI
In <DB2_HOME>/include
<DB2_HOME>/lib/db2api.lib
<DB2_HOME>/lib/db2cli.lib
 从上面可以看出,如果在windows下操纵MS的 数据库,使用MS VC++来编译OTL程序,就非常简单了,不用另外去找ODBC32.lib,VC的编译器中已经默认link到工程中了,具体请看如何编译OTL: http://otl.sourceforge.net/otl3_compile.htm
下面就让我们开始在 windows+VC 中使用 OTL 吧。先打开 ODBC 创建一个数据源 DSN (如 firebird ),指定需要操纵的数据库 ( MS Access2000) 。这个例子是在一个 Access 数据库中实现创建表、插入记录、更新记录和查询记录,代码实现:
不出意外,应该比较顺利地运行并输出结果:
f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name changed
f1=11, f2=NULL
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16

OTL4.0 中有一个问题,就是当 OTL boost date_time 库一起使用时,会出现命名冲突,主要是 OTLV4.h 头文件中使用 std 中的 isspace() 函数缺少命名空间,加上即可
line 4801:     while(isspace(*c)&&*c)
 modified to:    while(std::isspace(*c)&&*c)   // OK
line 12065:   while(isspace(*c)||(*c)=='(')++c;
 modified to:  while(std::isspace(*c)||(*c)=='(')++c;  // OK
这是在 windows 平台下进行的测试,还没在 Unix Linux 下进行,我第一次使用 OTL ,记下来所使用的过程,希望对后来者有所帮助。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值