天天看點

OTL使用ODBC操作SQLite資料庫

OTL (Oracle, Odbcand DB2-CLI Template Library)是操控關系資料庫的C++模闆庫,幾乎支援所有主流資料庫。操作Oracle可通過Oracle的OCI接口實作,操作DB2可通過CLI接口實作;而操作MS和其它資料庫,OTL提供了ODBC操作方式。另外,Oracle和DB2也可以由OTL使用ODBC操作。目前OTL的最新版本為4.0,參見http://otl.sourceforge.net/,下載下傳位址http://otl.sourceforge.net/otlv4_h2.zip。

本文介紹使用OTL操作SQLite資料庫的具體過程。

1、通過ODBC來通路操作SQLite資料庫需要安裝第三方元件庫的SQLite ODBC Driver,下載下傳位址http://www.ch-werner.de/sqliteodbc/。注意64位機器應下載下傳64位版本。

2、使用SQLiteExpertPro工具建立一個SQLite資料庫檔案,儲存在本地,建立表結構和示例資料,本例中為(id<int>, age<int>)。

3、進入“控制台—管理工具—ODBC 資料源(64 位)—系統DNS”頁籤,添加,選擇SQLite3 ODBC Driver建立資料源。設定相關資訊如下。注意将Database Name關聯到本地資料庫檔案。

OTL使用ODBC操作SQLite資料庫

4、建立測試工程項目,包含otlv4_h3.zip壓縮包中的頭檔案。測試代碼如下:

#include 
    
      
using namespace std; 
#include 
     
       
#include 
      
        
#include 
       
        

#define OTL_ODBC // Compile OTL 4.0/ODBC 
//#define OTL_ODBC_UNIX // for UnixODBC
//#define ODBCVER 0x0250
#include "otlv4.h" // include the OTL 4.0 header file

otl_connect db; // connect object

void insert() 
	// insert rows into table 
{  
	// open a stream with no implicit committing 
	otl_stream 
		o(1, // stream buffer size should be set to 1 
		"insert into aa values(:id
        
         ,:age
         
          )", // SQL statement db // connect object ); o<<2<<25; //int 2, age 25 } int main() { otl_connect::otl_initialize(); // initialize ODBC environment try{ db.rlogon("DSN=xuzhimin"); // connect to ODBC insert(); // insert records into the table } catch(otl_exception& p){ // intercept OTL exceptions cerr<
          
           <
           
          
         
        
       
      
     
    
           

5、更改目标平台為x64,編譯運作程式,即可往資料庫中寫入一條記錄。 6、要實作複雜的增删查改,可進一步學習otl相關文檔。