文档详情

OTL数据库访问技术

公****
实名认证
店铺
PPT
257.50KB
约38页
文档ID:601167168
OTL数据库访问技术_第1页
1/38

OTL,,数据库访问技术,北京神州数码思特奇信息技术股份有限公司,,研究院,OTL,应用开发,OTL,介绍,,OTL,“流”的概念,,OTL,的主要类,,OTL,的使用,,OTL,的编译,,OTL,参考资料,OTL,介绍,什么是,OTL:,OTL,是,Oracle,,Odbc,and DB2-CLI Template Library,的缩写,是一个操控关系数据库的,C++,模板库,它目前几乎支持所有的当前各种主流数据库,如下表所示:,数据库,访问接口,支持版本,Oracle,OCI,OCI7,、,OCI8,、,OCI8i,、,OCI9i,、,OCI10g,DB2,CLI,DB2 CLI,MS SQL Server,,、,Sybase,、,Informix,,、,MySQL,、,Interbase/Firebird,、,PostgreSQL,、,SQLite,、,SAP/DB,、,TimesTen,、,MS ACCESS,ODBC,ODBC2.5,、,ODBC3.x,备注,:,Oracle和DB2也可以由OTL间接使用ODBC的方式来进行操纵,OTL,的特点,:,优点,(1).,跨平台,,(2).,运行效率高,与,C,语言直接调用数据库,API,相当,,(3).,开发效率高,使用方便,,,繁在其内,简在其外,,,比,,,ADO.net,使用起来更简单,更简洁,,(4).,部署容易,不需要,ADO,组件,不需要,.net,,framework,等,缺点,(1).,只能在,C++,中使用,OTL,介绍,O,TL,“,流,”,的概念,任何,SQL,语句,,PL/SQL,块的调用或者是存储过程调用都可以用“流”的,input/output[,变量,],来表现。

使用,SQL,语句、,PL/SQL,块 或者是存储过程调用,都可以看作是带输入输出流的黑盒你可以不关心黑盒的内部工作(只需要依据黑盒的定义)需要关注的是黑盒的输入输出线O,TL,“,流,”,的概念,Example 1.,,,SELECT,语句有用于,WHERE,子句的标量,input,变量SELECT,语句同时定义了输出列,.,潜在的输出列是矢量参数,因为,SELECT,语句可能返回多行Example 2.,,,INSERT,语句往表中写数据, 即它有输入参数,,,其输入参数是标量,. UPDATE,语句亦是如此Example 3.,,,DELETE,从表中删除行,如需要输入删除条件,,DELETE,语句需有输入参数,其输入参数是标量Example 4.,,存储过程可能含有,input,和,(,或,)output,参数通常存储过程的参数是标量有一个特例:存储过程返回一个游标(,cursor,)(,ORACLE,)或者是结果集(,MSSQL,、,Sybase,),,,此时,output,参数是矢量Example 5.,,任意一个,PL/SQL,块都可能有标量的输入或矢量的输出参数O,TL,“,流,”,的概念,OTL,“,流,”,是缓存流,,从概念上讲,,OTL,“,流,”,有两个独立的缓存:,输入和输出,。

输入缓存由所有的输入变量共同组成同样的,输出缓存由所有的输出变量共同组成OTL,“,流,”,的操作,,,,(1). OTL,“,流,”,的操作与,C++,流一样,,,通过操作符,“<<”,和,“>>”,来进行操作,,,流的引用在操作符的左边s >> variable;,,,s << variable;,,两个箭头符号表示了数据流动方向,:,,,>> - -,从流到数据容器,(,变量,),,,<< - -,从数据容器,(,变量,),到流,,,(2). OTL,流需要用到,OTL,异常即任何,OTL,流都潜在的会抛,,otl_exception,,类型的异常为了截获异常并防止程序中断,请用,,相应,try & catch,块包装,OTL,代码段O,TL,,的主要类,主要类包括:,otl_stream, otl_connect, otl_exception,,,otl_stream,类,,,otl_stream,类 是,OTL,“,流,”,的概念的具体表现形式,,任何通过输入/输出参数使用SQL语句、PL/SQL 块 或者是存储过程调用,在C++的编程中都能通过,,otl_stream,,类来实现,。

其构造函数为,:,,(1) for Oracle 7/8/9/10:,,,otl_stream(,const,,int,,arr_size,, //,流的缓存大小,,,const char*,sqlstm,, // SQL,语句或,PL/SQL,块或存储过程,,,,otl_connect,& db,, // OTL,数据库连接对象,,const char*,ref_cur_placeholder,=0, //,游标引用占位符名称,,,const char*,sqlstm_label,=0 // SQL,语句标签,,);,O,TL,的主要类,(2)for ODBC/DB2-CLI:,,,otl_stream (,const,,int arr_size,, //,流的缓存大小,,,const char* sqlstm,, // SQL,语句或,PL/SQL,块或存储过程,,,otl_connect& db,, // OTL,数据库连接对象,,,const int implicit_select = otl_explicit_select,,,,,,const char* sqlstm_label=0 //SQL,语句标签,,);,,otl_connect,类,,,otl_connect,类封装了一系列有关数据库连接的功能,:,建立连接、断开连接、事务提交、事务回滚等等。

换言之,,otl_connect,是在,C++,编程中创建和使用数据库连接以及进行数据库事务管理的类,主要方法有,:,,,,(1) static int otl_initialize(const int threaded_mode=0);,,,该静态方法的主要功能是初始化,OTL,数据库环境,程序中第一次建立与数据库的连接之前,必须调用该方法一次,其后再建立与数据库的连接,就不需要调用该方法了如果程序是在多线程环境下访问数据库,,,参数,threaded_mode,需置为,1,另外在多线程环境下访问数据库,不要多个线程操作同一个,otl_connect,对象,,,除非该,ot_connect,对象有互斥锁机制2),void rlogon(const char* connect_str, const int auto_commit=0);,,,,该方法的主要功能是建立与数据库的连接参数,connect_str,是数据库连接配置字符串,,,有两种表达形式,,,,A.,,OTL4.0/OCIx,,,,■,”USER/PASSWORD”(,本地数据库,),,,■,”USER/PASSWORD@TNS_ALIAS”(,远程数据库,),,,,B.,OTL4.0/ODBC,和,OTL4.0/DB2_CLI,,,,■,”USER/PASSWORD@DSN”,,,■,”DSN=value;UID=value;PWD=value”,,,参数,auto_commit,设置数据库事务的提交模式,,auto_commit,设置为,1,,表,,示数 据库事务自动提交;,auto_commit,设置为,0,,表示数据库事务非自动提,,交,,auto_commit,缺省为,0,。

O,TL,的主要类,(3) void logoff(void);,,,,该方法的主要功能是断开与数据库的连接4) void commit(void);,,,,该方法的主要功能是提交数据库事务5) void rollback(void);,,,,该方法的主要功能是回滚数据库事务otl_exception,类,,,otl_exception,类用于描述,OTL,操作数据时抛出的异常,,,有,3,个主要的成,,员变量,:,,(1)unsigned char msg[1000];,,,该成员变量用于保存存异常的具体错误信息2)char stm_text[2048];,,,该成员变量用于保存导致发生异常错误的,SQL,语句3)char var_info[256];,,该成员变量用于保存导致发生异常错误的输入,/,输出变量O,TL,的主要类,O,TL,的使用,OTL,使用起来也很简单,使用不同的数据库连接,主要是根据需要在,,程序开始的,宏定义,来指定的OTL,是首先根据这个宏定义来初始化数据,,库连接环境OTL,中用来区分连接方式的宏定义主要有下面这些,:,,,OTL_ORA7, OTL_ORA8, OTL_ODBC, OTL_DB2_CLI, OTL_ODBC_MYSQL...,,不同的宏对应的数据库,API,,具体说明如下:,宏定义名,说明,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_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.,O,TL,的使用,SQL,使用举例:,,,//“,OtlSqlExample.cpp,”,,#include <,iostream,>,,using namespace std;,,#include <,stdio.h,>,,,,#define OTL_ORA9I,// Compile OTL 4.0/OCI9i,,,//#define OTL_UNICODE //Enable Unicode OTL for OCI9i,,,#include ,// include the OTL 4.0 header file,,,otl_connect,db;,// connect object,,,void insert();,,void,insertConstant,();,,void,insertBatch,();,,void,insertNoAutoCommit,();,,,void select();,,void update();,,void,updateNoAutoCommit,();,,void del();,,,int,main(),,{,,,otl_connect::otl_initialize,();,// initialize OCI environment,,try{,,,db.rlogon("dbuser/dbpwd,");,// connect to Oracle,O,TL,的使用,otl_cursor::direct_exec,,(,,db,,,"drop table person_tab",,,otl_exception::disabled // disable OTL exceptions,,); // drop table,,,otl_cursor::direct_exec,,(,,db,,,"create table person_tab(age number, name varchar2(30))",,); // create table,,,insert(); // insert one records into table,,insertConstant(); //constand insert sql,,insertBatch(); // insert batch records into table,,insertNoAutoCommit(); //insert no auto commit;,,select(); // select records from table,,update(); // update records in table,,updateNoAutoCommit(); // update no auto commit,,del(); // delete records from table,,},O,TL,的使用,catch(otl_exception& p) { // intercept OTL exceptions,,cerr<<,p.msg,<>r_age;,,i>>r_name;,,cout<<"age="<=0,SQL,执行成功,,,返回实际处理成功的记录数,.,,,,示例:,Examples(Oracle),,,otl_cursor::direct_exec,,(db, // connect object,,"create table person_tab(age number, name varchar2(30))",,); // create table,,,otl_cursor::direct_exec,,(db, // connect object,,"drop table persion_tab", // SQL statement,,otl_exception::disabled // disable OTL exceptions,,,// in other words, ignore any,,// database error,,); // drop table,O,TL,的使用,,long rpc=otl_cursor::direct_exec,,(db,//connect object,,“delete from persion_tab”);,,,,示例:,Examples(ODBC,DB2-CLI),,otl_cursor::direct_exec,,(db, // connect object,,"create table person_tab(age numeric, name varchar(30))",,); // create table,,,otl_cursor::direct_exec,,(db, // connect object,,"drop table persion_tab", // SQL statement,,otl_exception::disabled // disable OTL exceptions,,,// in other words, ignore any,,// database error,,); // drop table,,,,long rpc=otl_cursor::direct_exec,,(db,//connect object,,“delete from persion_tab”);,,O,TL,的使用,PL/SQL,块使用举例,:,,//“OtlPlsqlExample.cpp”,,#include ,,using namespace std;,,,#include ,,#define OTL_ORA9I // Compile OTL 4.0/OCI9i,,#include // include the OTL 4.0 header file,,,otl_connect db; // connect object,,,void plsql(void),,// invoking PL/SQL block,,{,,otl_stream o(50, // buffer size,,"begin ",," :A := :A+1; ",," :B := :C; ",,"end;",,,// PL/SQL block,,db // connect object,,);,,,O,TL,的使用,o.set_commit(0);,// set stream auto-commit off since,,// the block does not have any transactions,,// to commit,,,o<<1<<"Test String1";,// assigning :A = 1, :C = "Test String1",,,o<<2<<"Test String2";,// assigning :A = 2, :C = "Test String2",,,o<<3<<"Test String3";,// assigning :A = 3, :C = "Test String3",,,,o.flush();,// executing PL/SQL block 3 times,,,int a;,,char b[32];,,,while(!o.eof()){ // not end-of-data,,,o>>a>>b;,,cout<<"A="<

O,TL,的使用,存储过程使用举例:,,#include ,,using namespace std;,,,#include ,,#define OTL_ORA9I // Compile OTL 4.0/OCI9i,,#include // include the OTL 4.0 header file,,,otl_connect db; // connect object,,,void stored_proc(void),,{,,otl_stream o(1, // buffer size should be equal to 1 in case of,,// stored procedure call,,"begin my_proc(",,":a,",,":b, ",,":c ",,");end;",,,// stored procedure call,,db // connect object,,);,O,TL,的使用,,o.set_commit(0);,// set stream auto-commit off since,,// the stream does not generate transaction,,,o<<1<<“Test String1”;,// assigning :a = 1, :c = "Test String1",,,,o.flush();,,int a2;,,char b2[31];,,,,o>>a2>>b2;,,cout<<"A="<

O,TL,的使用,OTL,的编译,,在编译OTL的程序时,需要使用到相应的数据库API,这就要程序在,,编译时联接lib库文件,不同的数据库对应的lib文件所在位置各不相,,同,下面是分别在windows与Unix下的数据库API所需要的头文件及lib文,,件所在的位置列表:,API,API header files for Windows,API libraries for Windows,OCI7,\oci\include,\oci\lib\\ociw32.lib,OCI8,\oci\include,\oci\lib\\oci.lib,OCI8i,\oci\include,\oci\lib\\oci.lib,OCI9i,\oci\include,\oci\lib\\oci.lib,OCI10g,\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,\include,\lib\db2api.lib \lib\db2cli.lib,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//sqllib/include,-L//sqllib/lib -ldb2,OTL,的编译,OTL,实例,OTL,参考资料,OTL,最新版本为,4.0,,参见,:,,,,下载地址,:,,,,,目前提供有,377,个使用范例可参考,下载地址,:,,。

下载提示
相似文档
正为您匹配相似的精品文档