OTL--c++中连接数据库的方法

在C++代码中,可以很好地通过调用OTL类方法,实现客户端和oracle的交互。
下面是一个简单的开发步骤:
 1、定义OTL_ORA9I等宏定义
 2、引用otlv4.h头文件,这个头文件可以从网上下载:http://otl.sourceforge.net/otlv4_h.zip
 3、初始化:otl_connect::otl_initialize();
 4、使用otl_connect对象的server_attach、session_begin方法连接数据库
 5、使用otl_stream或者otl_nocommit_stream方法执行sql语句。
       声明对象:
               otl_nocommit_stream osSeldb; 
                char *sql="select * from dual";
       执行sql:osSeldb.open(50, strSqldb.c_str(), db);
          执行取数据、插入数据等操作。。。
       关闭对象: osSeldb.close();
 
   注意:otl_stream,默认情况下,语句执行完毕后,会自动提交数据库,需慎用。
  
开发举例:
  
  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #define OTL_ORA9I // Compile OTL 4.0/OCI9i
  5. #define OTL_ORA_UTF8 // Enable UTF8 OTL for OCI9i
  6. #define OTL_STL
  7. #define OTL_UNCAUGHT_EXCEPTION_ON
  8. #define OTL_STL_NOSTD_NAMESPACE
  9. #define OTL_ORA9I
  10. #include <otlv4.h> // include the OTL 4.0 header file
  11. otl_connect db; // connect object
  12. void select()
  13. {
  14.   otl_stream i ( 1 , // buffer size
  15.                   "select to_char(cust_id),to_char(sub_bill_id),'12345 ','fftest' from user_table where rownum<100 " ,
  16.                                            // SELECT statement
  17.                   db // connect object
  18.    );
  19.   char f3[100];
  20.   int nLoop;
  21.   while ( !i.eof() )
  22.   {//循环取出每一行
  23.       for ( nLoop = 0; nLoop < *( i.ov_len ); nLoop++ )
  24.       {//循环取出每列
  25.           i >> f3;
  26.           cout << f3 << ",";
  27.       }
  28.       cout << endl;
  29.   }
  30. }

  31. int main()
  32. {
  33.   otl_connect::otl_initialize(); // initialize OCI environment
  34.   try
  35.   {
  36.       db.server_attach( "tnsname" );//
  37.       db.session_begin( "userName" ,"password" ); // connect to Oracle
  38.       select(); // select records from table
  39.   }
  40.   catch ( otl_exception& p )
  41.   {
  42.       // intercept OTL exceptions
  43.       cerr << p.msg << endl; // print out error message
  44.       cerr << p.stm_text << endl; // print out SQL that caused the error
  45.       cerr << p.var_info << endl; // print out the variable that caused the error
  46.   }
  47.   db.logoff(); // disconnect from Oracle
  48.   return 0;
  49. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值