分享

在跨平台的C++程序中使用OTL来操作不同的数据库 - roger007的专栏 - CSD...

 haliiz 2010-11-22
本文主要介绍了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./,下载地址http://otl./otlv4_h.zip
优点:
      a. 跨平台
      b. 运行效率高,与C语言直接调用API相当
      c. 开发效率高,起码比ADO.net使用起来更简单,更简洁
      d. 部署容易,不需要ADO组件,不需要.net framework 等
缺点:
      a. 说明文档以及范例不足够丰富(暂时性的)
     其实现在它提供有377个使用范例可参考,下载地址:ttp://otl./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 Windows
 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
 
 
 在Unix平台中:
 从上面可以看出,如果在windows下操纵MS的 数据库,使用MS VC++来编译OTL程序,就非常简单了,不用另外去找ODBC32.lib,VC的编译器中已经默认link到工程中了,具体请看如何编译OTL:http://otl./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,记下来所使用的过程,希望对后来者有所帮助。
 
 
 
 
 
API
 API header files for Unix
 API libraries for Unix
 
OCI7
 -I$(ORACLE_HOME)/rdbms/demo
 -I$(ORACLE_HOME)/rdbms/public
 -L$(ORACLE_HOME)/lib/ -lclntsh
 
OCI8
 -I$(ORACLE_HOME)/rdbms/demo 
-I$(ORACLE_HOME)/rdbms/public
 -L$(ORACLE_HOME)/lib/ -lclntsh
 
OCI8i
 -I$(ORACLE_HOME)/rdbms/demo
  -I$(ORACLE_HOME)/rdbms/public
 -L$(ORACLE_HOME)/lib/ -lclntsh
 
OCI9i
 -I$(ORACLE_HOME)/rdbms/demo
 -I$(ORACLE_HOME)/rdbms/public
 -L$(ORACLE_HOME)/lib/ -lclntsh
 
OCI10g
 -I$(ORACLE_HOME)/rdbms/demo
  -I$(ORACLE_HOME)/rdbms/public
 -L$(ORACLE_HOME)/lib/ -lclntsh
 
ODBC
 ODBC bridge specific
 ODBC bridge specific
 
DB2 CLI
 -I/<DB2_HOME>/sqllib/include
 -L/<DB2_HOME>/sqllib/lib -ldb2
 
 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/roger_77/archive/2006/03/23/633132.aspx

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多