Oracle 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个使用范例可参考,下载地址: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_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 /oci/include /oci/lib//ociw32.lib OCI8 In /oci/include /oci/lib//oci.lib OCI8i In /oci/include /oci/lib//oci.lib OCI9i In /oci/include /oci/lib//oci.lib OCI10g In /oci/include /oci/lib//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 /include /lib/db2api.lib /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 using namespace std; #include #include #include #define OTL_ODBC // Compile OTL 4.0/ODBC // #define OTL_ODBC_UNIX // 如果在Unix下使用UnixODBC,则需要这个宏 #include // 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,:f2)", // SQL statement db // connect object ); char tmp[32]; for(int i=1;i<=100;++i){ sprintf(tmp,"Name%d",i); o< " " WHERE f1=:f1", // UPDATE statement db // connect object ); o<<"Name changed"< 1 "select * from test_tab " "where f1>=:f11 " " and f1<=:f12*2", // SELECT statement db // connect object ); // create select stream int f1; char f2[31]; i<>f1; cout<<"f1="<

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值