转自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 数据库中实现创建表、插入记录、更新记录和查询记录,代码实现: #include <iostream> using namespace std; #include <stdio.h> #include <string.h> #include <stdlib.h> #define OTL_ODBC // Compile OTL 4.0/ODBC // #define OTL_ODBC_UNIX // 如果在Unix下使用UnixODBC,则需要这个宏 #include <otlv4.h> // include the OTL 4.0 header file otl_connect db; // connect object void insert() // insert rows into table { // open a stream with no implicit committing otl_stream o(1, // stream buffer size should be set to 1 "insert into test_tab values(:f1<int>,:f2<char[31]>)", // SQL statement db // connect object ); char tmp[32]; for(int i=1;i<=100;++i){ sprintf(tmp,"Name%d",i); o<<i<<tmp; } } void update(const int af1) // insert rows into table { otl_stream o(1, // buffer size "UPDATE test_tab " " SET f2=:f2<char[31]> " " WHERE f1=:f1<int>", // UPDATE statement db // connect object ); o<<"Name changed"<<af1; o<<otl_null()<<af1+1; // set f2 to NULL } void select(const int af1) { otl_stream i(50, // buffer size may be > 1 "select * from test_tab " "where f1>=:f11<int> " " and f1<=:f12<int>*2", // SELECT statement db // connect object ); // create select stream int f1; char f2[31]; i<<af1<<af1; // Writing input values into the stream while(!i.eof()){ // while not end-of-data 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("UID=scott;PWD=tiger;DSN=firebird"); // connect to ODBC //或者使用下面的连接语句方式。 // db.rlogon("scott/tiger@firebird"); // connect to ODBC, alternative format // of connect string 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 update(10); // update records in 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 the database return 0; } 不出意外,应该比较顺利地运行并输出结果: 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 ,记下来所使用的过程,希望对后来者有所帮助。