在跨平台的C++程序中使用OTL来操作不同的数据库zt

13 篇文章 0 订阅
13 篇文章 0 订阅
在跨平台的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来操作的方式。当然OracleDB2也可以由OTL间接使用ODBC的方式来进行操纵。

MS Windows and Unix 平台下,OTL目前支持的数据库版本主要有:Oracle 7 (直接使用 OCI7), Oracle 8 (直接使用 OCI8), Oracle 8i (直接使用OCI8i), Oracle 9i (直接使用OCI9i), Oracle 10g ( 直接使用OCI 10g), DB2 ( 直接使用DB2 CLI), ODBC 3.x ,ODBC 2.5OTL最新版本为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个使用范例可参考,下载地址:ttp://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_ORA 10G

for OCI 10g. All code that compiles and works  under #define OTL_ORA7, OTL_ORA8, OTL_ORA8I, OTL_ORA9I, should work with OTL_ORA 10G.

OTL_ORA 10G_R2

for OCI 10g, Release 2 (Oracle 10.2). All code that compiles and works  under #define OTL_ORA7, OTL_ORA8, OTL_ORA8I, OTL_ORA9I, and OTL_ORA 10G should work with OTL_ORA 10G_R2 .

我们在编译OTL的程序时,需要使用到相应的数据库API,这就要程序在编译时联接lib库文件,不同的数据库对应的lib文件所在位置各不相同,下面是分别在windowsUnix下的数据库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

OCI 10g

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.libVC的编译器中已经默认link到工程中了,具体请看如何编译OTLhttp://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=Name 8
f1=9, f2=Name
9
f1=10, f2=Name changed
f1=11, f2=NULL
f1=12, f2=Name
12
f1=13, f2=Name
13
f1=14, f2=Name
14
f1=15, f2=Name
15
f1=16, f2=Name16


OTL4.0
中有一个问题,就是当OTLboostdate_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平台下进行的测试,还没在UnixLinux下进行,我第一次使用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

OCI 10g

-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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值