釋出
2013年3月7日

C++ SQLite 包裝器

評分:3.7/5 (157 票)
*****

關於 SQLite

這是一個用 C++ 編寫的簡單 SQLite 包裝器。SQLite 是一個嵌入式 SQL 資料庫引擎,經過全面測試。SQLite 沒有單獨的伺服器程序。SQLite 直接讀寫普通磁碟檔案。
SQLite 資料庫的描述請訪問 這裡。
Windows OS 版 SQLite 可在此 下載。

SQLite 包裝器

此原始碼提供建立資料庫和在行動式資料庫中執行查詢的簡單方法。建立的 SQLite 資料庫檔案是平臺獨立的,可以在 Windows 或 Linux OS 上使用或複製。

SQLiteDB 類

此類包含 SQLite 包裝器的方法。

開啟連線的方法接受兩個引數,第一個是資料庫檔名,可以是 *.db 格式,第二個是 DB 檔案將建立的目錄。如果 DB 檔案已存在,則會開啟它,否則會建立一個新檔案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class SQLiteDB : public IResult
{

public:
	SQLiteDB();
	~SQLiteDB();
	
	/*Open Connection*/
	bool OpenConnection(string DatabaseName,string DatabaseDir); 


	/*Close Connection*/
	void CloseConnection();

	/*Query Wrapper*/
	/*For large insert operation Memory Insert option for SQLLITE dbJournal*/
	void BeginTransaction();
	void CommitTransection();

	/*This Method called when SELECT Query to be excuted. 
	Return RESULTSET class pointer on success else NULL of failed*/
	IResult*  ExcuteSelect(const char *Query);

	/*This Method called when INSERT/DELETE/UPDATE Query to be excuted. 
	Return UINT count of effected data on success*/
	UINT	    Excute(const char *Query);

	/*Get Last Error of excution*/
	string GetLastError();

	/*Return TRUE if databse is connected else FALSE*/
	bool  isConnected() ;	


protected:
	/*SQLite Connection Object*/
	typedef struct SQLLITEConnection
	{
		string		 SQLiteDatabaseName;   //Database Name
		string		 SQLiteDBPath;		   //Databse File Dir
		sqlite3		 *pCon;				   //SQLite Connection Object
		sqlite3_stmt *pRes;				   //SQLite statement object 
	}SQLITECONNECTIONOBJECT;

	//SQLite Connection Details
	SQLITECONNECTIONOBJECT	 *pSQLiteConn;

	/*Sync Database in Case of Multiple Threads using class object*/
	SyncDB					 *Sync;	

	bool	m_bConnected;      /*Is Connected To DB*/
	bool    m_bConsole;	       /*If Console Application*/
	string  m_strLastError;    /*Last Error String*/
	int	    m_iColumnCount;    /*No.Of Column in Result*/


private:
	/*This function return of count of column 
	  present in result set of last excueted query*/
	int	    GetColumnCount();

	/*Get the next coloumn name*/
	const char* NextColomnName(int iClmnCount);
	
	/*This function returns TRUE if still rows are 
	der in result set of last excueted query FALSE 
	if no row present*/
	bool  Next();

	/*Get the next coloumn data*/
	const char*  ColomnData(int clmNum);

	/*RELEASE all result set as well as RESET all data*/
	void Release(); 

};


可以在方法 IResult* ExcuteSelect(const char *Query); 中執行 select 查詢,該方法返回 IResult 介面類的物件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
/*Interface class for Result data of query*/
class IResult
{
public:
	/*This function return of count of column 
	  present in result set of last excueted query*/
	virtual int	    GetColumnCount() = 0;

	/*Get the next coloumn name*/
	virtual const char* NextColomnName(int iClmnCount) = 0;
	
	/*This function returns TRUE if still rows are 
	der in result set of last excueted query FALSE 
	if no row present*/
	virtual bool  Next() = 0;

	/*Get the next coloumn data*/
	virtual const char*  ColomnData(int clmNum) = 0;

	/*RELEASE all result set as well as RESET all data*/
	virtual void Release() = 0;
};


如何使用

測試檔案 SQLiteTest.cpp 使用 SQLLite Wrapper 類實現。對於大量資料插入,使用 void void BeginTransaction();void CommitTransection(); 方法。如果不使用這些方法,多次插入將比平時花費更長的時間。

IResult 類具有純虛方法,用於提供讀取結果資料的介面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Get Data From DB
	IResult *res=pSQLite->ExcuteSelect("Select * from test;");
	if(!res)
	  cout<<"\n Error:"<<pSQLite->GetLastError().c_str();
	
	else
	{
		//Get Column Count
		int i = res->GetColumnCount();
		
		//Print Colomn Name
		for(int k=0;k<i;k++)
		{
			printf("%s\t",res->NextColomnName(k));	
		}
		
		cout<<endl;

		//Print Result
		while(res->Next())
		{
			for(int k=0;k<i;k++)
				printf("%s\t",res->ColomnData(k));		
			cout<<endl;
		}

		//release Result Data
		res->Release();
	}

附件:[SQLiteTest.cpp] [SQLLiteWrapper.cpp] [SQLLiteWrapper.h]