操作環境:
1. 作業系統:Windows XP Professional with SP2。
2. 程式設計環境:Visual C++ 6.0 with SP6。
3. 資料庫環境:Access 2003。
OTL簡介:
OTL 是 Oracle, Odbc and DB2-CLI Template Library 的縮寫,是一個C++編譯中操控關系資料庫的模闆庫,它目前幾乎支援所有的目前各種主流資料庫,例如Oracle, MS SQL Server, Sybase, Informix, MySQL, DB2, Interbase / Firebird, PostgreSQL, SQLite, SAP/DB, TimesTen, MS ACCESS等等。OTL中直接操作Oracle主要是通過Oracle提供的OCI接口進行,進行操作DB2資料庫則是通過CLI接口來進行,至于MS的資料庫和其它一些資料庫,則OTL隻提供了ODBC來操作的方式。當然Oracle和DB2也可以由OTL間接使用ODBC的方式來進行操縱。
優點:
a. 跨平台
b. 運作效率高,與C語言直接調用API相當
c. 開發效率高,起碼比ADO.net使用起來更簡單,更簡潔
d. 部署容易,不需要ADO元件,不需要.net framework 等
缺點:
a. 說明文檔以及範例不足夠豐富(暫時性的)
建立資料源
1.依次點選“開始->控制台”,打開“控制台”界面,輕按兩下“管理工具”,然後再輕按兩下“資料源(ODBC)”,就打開了“ODBC資料源管理器”,選擇“系統DSN”。
2.單擊“添加”,彈出“建立新資料源”對話框,選擇“Microsoft Access Driver(*.mdb)”。
3.點選“完成”,彈出“ODBC Microsoft Access安裝”對話框,單擊“建立”,開始建立資料庫,彈出“建立資料庫”對話框,添加資料庫名稱my_db和選擇資料庫存放目錄,單擊“确定”,建立完成,然後添加資料源名:my_db。點選“确定”。
4.然後在系統資料源中就有我們剛才添加的資料源。
5.單擊“确定”,完成資料源的建立。
OTL程式設計
下面我們用一個執行個體來說明:
1. 建立資料表:TestTable ( ColumA int , ColumB varchar(50),ColumC varchar(50) )
2. 插入100條資料,ColumA 為資料的 id 範圍:0-99 , ColumB=”Test Data %d” , 其中 %d=id 。
3. 删除表中ColumA 中小于10和大于90的資料。
4. 将ColumA為3的倍數的記錄中ColumC更新為ColumB的内容。
具體代碼為:
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define OTL_ODBC // 編譯 OTL 4.0/ODBC
// #define OTL_ODBC_UNIX // 如果在Unix下使用UnixODBC,則需要這個宏
#include "otlv4.h" // 包含 OTL 4.0 頭檔案
otl_connect db; // 連接配接對象
//此函數完成插入100條資料,ComulA為資料的id,範圍為0-99,
//ColumB="Test Data %d",其中%d=id
void insert()
// 向表中插入行
{
// 打開一個通用的流,以模闆的方式向表中插入多項資料
otl_stream
o(1, // 流的緩沖值必須設定為1
"insert into TestTable values(:f1<int>,:f2<char[50]>,:f3<char[50]>)",
// SQL 語句
db // 連接配接對象
);
char tmp1[32];
char tmp2[30];
for(int i=0;i<100;++i){
sprintf(tmp1,"Test Data %d",i);
sprintf(tmp2,"");
o<<i<<tmp1<<tmp2;
}
//此函數完成删除表中ColumA中小于10和大于90的資料
void delete_rows()
long rpc=otl_cursor::direct_exec(db,"delete from TestTable where ColumA<10 or ColumA>90");
// rpc是作用效果的傳回值,otl_cursor::direct_exec為直接執行sql語句
cout<<"Rows deleted: "<<rpc<<endl;
//此函數完成将ColumA為3的倍數的記錄中ColumC更新為ColumB的内容
void update()
// 更新表
{
otl_stream
o(1, // 緩沖值
"UPDATE TestTable "
" SET ColumC=:f2<char[50]> "
" WHERE ColumA=:f1<int>",
// UPDATE 語句
db // 連接配接對象
otl_stream c(1,"select ColumB from TestTable where ColumA=:f3<int>",db);
char temp[10];
for(int i=10;i<91;i++)
if(i%3==0)
{
c << i;
c >> temp;
o << temp << i;
}
int main()
otl_connect::otl_initialize(); // 初始化 ODBC 環境
try{
db.rlogon("UID=scott;PWD=tiger;DSN=my_db"); // 連接配接到 ODBC
//或者使用下面的連接配接語句方式。
// db.rlogon("scott/tiger@firebird"); // connect to ODBC, alternative format
// of connect string
otl_cursor::direct_exec
(
db,
"drop table TestTable",
otl_exception::disabled // disable OTL exceptions
); // drop table
//這裡完成表的建立
"create table TestTable(ColumA int, ColumB varchar(50),ColumC varchar(50))"
); // create table
insert(); // insert records into the table
// update(10); // update records in the table
delete_rows();
update();
catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.var_info<<endl; // print out the variable that caused the error
db.logoff(); // disconnect from the database
return 0;