Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88887
  • 博文数量: 31
  • 博客积分: 886
  • 博客等级: 准尉
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-02 10:45
文章分类

全部博文(31)

文章存档

2011年(31)

我的朋友

分类: C/C++

2011-01-18 13:01:21

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

相关的宏定义列表 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,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,:f2)",
  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. }
阅读(2645) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~