C++使用OTL连接数据库.doc_第1页
C++使用OTL连接数据库.doc_第2页
C++使用OTL连接数据库.doc_第3页
C++使用OTL连接数据库.doc_第4页
C++使用OTL连接数据库.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

OTL是一个纯C+的通用数据库连接模板库,可以支持各种当下流行的数据库,如Oracle,Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite, MS ACCESS, Firebird等等.它是一个跨平台类库,在MS Windows, Linux/Unix/Mac OS X 都可以使用。下载网址:/OTL使用简单, 只要头文件中包含有: #include otlv4.h 就可,实际上整个OTL就一个.H的文件,使用起来极为的方便。OTL使用方法:1、首先指定要连接的数据库类型,OTL用宏定义来指定要连接的数据库类型。OTL会根据这个宏定义来初始化数据库连接的环境。相关的宏定义列表 /otl3_compile.htm如: #define OTL_ORA8I 表示连接Oracle 8i 数据库。2、例子#include using namespace std;#include #define OTL_ORA8I / 指定连接的数据库类型#include / include the OTL 4 header fileotl_connect db; / 定义数据库实例void insert()/ 添加记录otl_stream o(50, / buffer size insert into test_tab values(:f1,:f2), / SQL statement db / connect object );char tmp32;for(int i=1;i= 1400) / VC+ 8.0 or higher sprintf_s(tmp,sizeof(tmp),Name%d,i);#else sprintf(tmp,Name%d,i);#endif#else sprintf(tmp,Name%d,i);#endif ostatic_cast(i)=:f and f1=:f*2; end;, / SELECT statement via referenced cursor db, / connect object :cur1 / referenced cursor placeholder name ); / create select streamotl_column_desc* desc;int desc_len;i4; / Before the child SELECT statement can be described, / the PL/SQL master block needs to be executed. / Forcing the execution of the blcok by entering its input / patameter.desc=i.describe_select(desc_len);for(int n=0;ndesc_len;+n) cout= COLUMN #n+1 =endl; coutname=endl; coutdbtype=descn.dbtypeendl; coutotl_var_dbtype=descn.otl_var_dbtypeendl; coutdbsize=descn.dbsizeendl; coutscale=descn.scaleendl; coutprec=descn.precendl; coutnullok=descn.nullokendl;int main()otl_connect:otl_initialize(); /初始化Oracle环境try db.rlogon(scott/tiger); / 连接Oracle 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 number, f2 varchar2(30) ); / create table insert(); select();catch(otl_exception& p) / intercept OTL exceptions cerrp.msgendl; / print out error message cerrp.stm_textendl; / print out SQL that caused the error cerrp.var_infoendl; / print out the variable that caused the errordb.logoff(); /断开数据库连接return 0;#include stdafx.h#include using namespace std; #include /#define OTL_ORA10G#define OTL_ORA9I / Compile OTL 4.0/OCI9i#define OTL_ORA_UTF8 / Enable UTF8 OTL for OCI9i#include / include the OTL 4.0 header file#pragma comment(lib, oci.lib)otl_connect db; / connect object#define CONNECTSTRING test/1234502char strSelectSql100 = select f1,f2,f3 from test_tab;char strDeleteSql100 = delete from test_tab where f1 = 4 ;char strUpdate150 = update test_tab set f2 = Olympiad in china , f3 = to_date(2008-08-08 08:08:08,yyyy-mm-dd hh24:mi:ss) where f1 = 5 ;void insert() otl_stream o;o.open(1,INSERT INTO TEST_TAB(f1,f2,f3)values(4,number one,to_date(2007-09-06 13:49:50,yyyy-mm-dd hh24:mi:ss),db); o.close();o.open(1,INSERT INTO TEST_TAB(f1,f2,f3)values(5,number one,to_date(2007-09-06 13:49:50,yyyy-mm-dd hh24:mi:ss),db);void deleteR()otl_stream o(1,strDeleteSql,db);void update()otl_stream o(1,strUpdate,db);void select() otl_stream o(1,strSelectSql,db);int count = 0;int myf1;char myf230 = ;char myf330 = ;otl_datetime datetime; while(!o.eof() omyf1;omyf2;odatetime;count+;sprintf(myf3,%d-%02d-%02d %02d:%02d:%02d,datetime.year,datetime.month,datetime.day,datetime.hour,datetime.minute,datetime.second);coutf1: myf1, f2: myf2, 时间: myf3, 当前行数: countendl;return ;int main() otl_connect:otl_initialize(); / initialize OCI environment trydb.rlogon(CONNECTSTRING); / connect to Oracleotl_cursor:direct_exec(db,drop table test_tab,otl_exception:disabled); / drop tableotl_cursor:direct_exec(db,create table test_tab(f1 number, f2 nvarchar2(30), f3 date ); / create table /before delete .coutbefore delete . endl;insert(); / insert records into table select(); / select records from tablecoutafter delete . delete condition: f1 = 4 endl;deleteR();select();coutendl;coutbefore upate . endl;select();coutafter upate . update condition: f1 = 5 endl;update();select();catch(otl_exception& p) / intercept OTL exceptions

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论