C++类库:OTL通用的数据库连接类库
生活随笔
收集整理的這篇文章主要介紹了
C++类库:OTL通用的数据库连接类库
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
From: http://www.cppblog.com/lovefeel2004/archive/2010/06/02/116946.aspx
OTL是一個純C++的通用數(shù)據(jù)庫連接模板庫,可以支持各種當下流行的數(shù)據(jù)庫,如Oracle,Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite,??MS ACCESS, Firebird等等.它是一個跨平臺類庫,在MS Windows, Linux/Unix/Mac OS X 都可以使用。
下載網(wǎng)址:http://otl.sourceforge.net/
OTL使用簡單, 只要頭文件中包含有: #include "otlv4.h" 就可,實際上整個OTL就一個.H的文件,使用起來極為的方便。
OTL使用方法:
1、首先指定要連接的數(shù)據(jù)庫類型,OTL用宏定義來指定要連接的數(shù)據(jù)庫類型。OTL會根據(jù)這個宏定義來初始化數(shù)據(jù)庫連接的環(huán)境。
相關的宏定義列表?http://otl.sourceforge.net/otl3_compile.htm
如: #define OTL_ORA8I 表示連接Oracle 8i 數(shù)據(jù)庫。
?
2、例子
#include <iostream> using namespace std; #include <stdio.h>#define OTL_ORA8I // 指定連接的數(shù)據(jù)庫類型 #include <otlv4.h> // include the OTL 4 header fileotl_connect db; // 定義數(shù)據(jù)庫實例void insert() // 添加記錄{otl_stream o(50, // buffer size"insert into test_tab values(:f1<float>,:f2<char[31]>)",// SQL statementdb // connect object);char tmp[32];for(int i=1;i<=100;++i){ #if defined(_MSC_VER) #if (_MSC_VER >= 1400) // VC++ 8.0 or highersprintf_s(tmp,sizeof(tmp),"Name%d",i); #elsesprintf(tmp,"Name%d",i); #endif #elsesprintf(tmp,"Name%d",i); #endifo<<static_cast<float>(i)<<tmp;} }//查詢記錄void select() {otl_stream i(50, // buffer size"begin "" open :cur1 for "" select * from test_tab "" where f1>=:f<int> and f1<=:f*2; ""end;", // SELECT statement via referenced cursordb, // connect object":cur1" // referenced cursor placeholder name);// create select streamotl_column_desc* desc;int desc_len;i<<4; // 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;n<desc_len;++n){cout<<"========== COLUMN #"<<n+1<<" ==========="<<endl;cout<<"name="<<desc[n].name<<endl;cout<<"dbtype="<<desc[n].dbtype<<endl;cout<<"otl_var_dbtype="<<desc[n].otl_var_dbtype<<endl;cout<<"dbsize="<<desc[n].dbsize<<endl;cout<<"scale="<<desc[n].scale<<endl;cout<<"prec="<<desc[n].prec<<endl;cout<<"nullok="<<desc[n].nullok<<endl;}}int main() {otl_connect::otl_initialize(); // 初始化Oracle環(huán)境try{db.rlogon("scott/tiger"); // 連接Oracleotl_cursor::direct_exec(db,"drop table test_tab",otl_exception::disabled // disable OTL exceptions); // drop tableotl_cursor::direct_exec(db,"create table test_tab(f1 number, f2 varchar2(30))"); // create tableinsert(); select();}catch(otl_exception& p){ // intercept OTL exceptionscerr<<p.msg<<endl; // print out error messagecerr<<p.stm_text<<endl; // print out SQL that caused the errorcerr<<p.var_info<<endl; // print out the variable that caused the error}db.logoff(); //斷開數(shù)據(jù)庫連接return 0; } ?
總結
以上是生活随笔為你收集整理的C++类库:OTL通用的数据库连接类库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Asp.net mvc 知多少(一)
- 下一篇: java 知网 语义 相似度,基于知网的