c++数据库访问OTL和DTL

7 篇文章 0 订阅
OTL :http://otl.sourceforge.net/
DTL:http://dtemplatelib.sourceforge.net/
OTL例子:http://otl.sourceforge.net/otl3_examples.htm

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会根据这个宏定义来初始化数据库连接的环境。

相关的宏定义列表 http://otl.sourceforge.net/otl3_compile.htm

如: #define OTL_ORA8I 表示连接Oracle 8i 数据库。
------------------------------------------------------------------------------------
  1. 使用otl访问sql server

  2. #include <iostream>
  3. using namespace std;

  4. #include <stdio.h>
  5. #define OTL_ODBC_MSSQL_2008 // Compile OTL 4.0/ODBC for MS SQL 2008
  6. #define OTL_MAP_SQL_GUID_TO_CHAR
  7. #include <otlv4.h> // include the OTL 4.0 header file

  8. otl_connect db; // connect object

  9. void insert()
  10. // insert rows into table
  11. {
  12.  otl_stream o(5, // buffer size
  13.               "insert into test_tab values(:f1<int>,newid())",
  14.                  // SQL statement
  15.               db // connect object
  16.              );

  17.  for(int i=1;i<=13;++i)
  18.   o<<i;
  19. }

  20. void select()
  21. {
  22.  otl_stream i(5, // buffer size
  23.               "select * from test_tab",
  24.                  // SELECT statement
  25.               db // connect object
  26.              );
  27.    // create select stream

  28.  otl_stream o(7, // buffer size
  29.               "insert into test_tab2 values(:f1<int>,:f2<char[37]>)",
  30.                  // INSERT statement
  31.               db // connect object
  32.              );
  33.    // create insert stream
  34.  o.set_commit(0); // turnin off the otl_stream's autocommit flag
  35.  
  36.  int f1;
  37.  char f2[37];

  38.  while(!i.eof()){ // while not end-of-data
  39.   i>>f1>>f2;
  40.   cout<<"f1="<<f1<<", f2="<<f2<<endl;
  41.   o<<f1<<f2;
  42.  }

  43.  o.flush(); // flushing the otl_stream's buffer
  44.  db.commit(); // committing transaction

  45. }

  46. int main()
  47. {
  48.  otl_connect::otl_initialize(); // initialize ODBC environment
  49.  try{

  50.   db.rlogon("UID=scott;PWD=tiger;DSN=mssql2008"); // connect to ODBC

  51.   otl_cursor::direct_exec
  52.    (
  53.     db,
  54.     "drop table test_tab",
  55.     otl_exception::disabled // disable OTL exceptions
  56.    ); // drop table

  57.   otl_cursor::direct_exec
  58.    (
  59.     db,
  60.     "create table test_tab(f1 int, f2 uniqueidentifier)"
  61.     ); // create table

  62.   otl_cursor::direct_exec
  63.    (
  64.     db,
  65.     "drop table test_tab2",
  66.     otl_exception::disabled // disable OTL exceptions
  67.    ); // drop table

  68.   otl_cursor::direct_exec
  69.    (
  70.     db,
  71.     "create table test_tab2(f1 int, f2 uniqueidentifier)"
  72.     ); // create table


  73.   insert(); // insert records into table
  74.   select(); // select records from test_tab and insert them into test_tab2

  75.  }

  76.  catch(otl_exception& p){ // intercept OTL exceptions
  77.   cerr<<p.msg<<endl; // print out error message
  78.   cerr<<p.stm_text<<endl; // print out SQL that caused the error
  79.   cerr<<p.sqlstate<<endl; // print out SQLSTATE message
  80.   cerr<<p.var_info<<endl; // print out the variable that caused the error
  81.  }

  82.  db.logoff(); // disconnect from ODBC
  83.  return 0;

  84. }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值