微軟WP7本機(jī)數(shù)據(jù)庫解決方案之SQLite
無論是從用戶的角度來看還是從開發(fā)人員的角度來看,Windows Phone 7給我們帶來眾多的新的和令人振奮的功能。與此同時(shí),當(dāng)前的Windows Phone 7系列也的確存在令許多用戶失望的缺憾。一個(gè)代表性的遺憾便是,Windows Phone 7缺乏本地?cái)?shù)據(jù)庫API支持—當(dāng)前的Windows Phone 7操作系統(tǒng)僅提供通過XML、客戶端獨(dú)立存儲(chǔ)和云存儲(chǔ)等幾種有限的數(shù)據(jù)訪問支持。那么,就本地?cái)?shù)據(jù)庫功能來說,我們真的有沒有其他的選擇了嗎?當(dāng)然不是這樣。如今各種開源社團(tuán)如火如荼,已經(jīng)有一些開發(fā)人員和團(tuán)隊(duì)一直努力在填補(bǔ)這一方面的空白。
請(qǐng)注意,盡管目前已有多個(gè)嘗試實(shí)現(xiàn)了Windows Phone 7本地?cái)?shù)據(jù)庫支持,但***,這些系統(tǒng)都需要運(yùn)行于Windows Phone 7提供的獨(dú)立存儲(chǔ)基礎(chǔ)之上。在本系列文章中,我將向你介紹上述成員之一-Sqlite Client for Windows Phone。這是一種新的,功能強(qiáng)大且易于使用的Windows Phone 7本機(jī)數(shù)據(jù)庫解決方案,該系統(tǒng)公布于著名的開源網(wǎng)站CodePlex。篇幅所限,我僅介紹在Windows Phone 7系統(tǒng)中使用Sqlite Client for Windows Phone編程的部分技巧。
[備注]本系列文章中的案例程序調(diào)試環(huán)境包括:
- Windows 7;
- .NET 4.0;
- Visual Studio 2010;
- Windows Phone Developer Tools RTW;
- Sqlite Client for Windows Phone (http://sqlitewindowsphone.codeplex.com/);
- (推薦) sqlite-manager (http://code.google.com/p/sqlite-manager/);
- (推薦) MVVM Light Toolkit (http://mvvmlight.codeplex.com/).
一、Sqlite Client for Windows Phone簡介
大家都知道,SQLite是一個(gè)著名的開源的嵌入式的數(shù)據(jù)庫系統(tǒng),目前已經(jīng)提供對(duì)于iOS和Android的良好支持。在此,應(yīng)當(dāng)讓我們感謝Dan Ciprian Ardelean,是他帶給我們WP7版本的SQLite-C#-SQLite!最近幾個(gè)月,作者對(duì)早期的版本又進(jìn)行了更新,得到一個(gè)功能更強(qiáng)大和更容易使用的解決方案,改名為Sqlite Client for Windows Phone,在知名的開源網(wǎng)站CodePlex上發(fā)布,網(wǎng)址是http://sqlitewindowsphone.codeplex.com/releases。
▲圖1. 下載Sqlite Client for Windows Phone的界面截圖
Sqlite Client for Windows Phone進(jìn)行大范圍的更新,如提供對(duì)于布爾類型、大型數(shù)據(jù)(Blobs)以及事務(wù)的全面支持,此外,下載包中還一并提供了一個(gè)全面的示例,供開發(fā)者學(xué)習(xí)之用。
▲圖2. Sqlite Client for Windows Phone源碼工程及示例工程架構(gòu)
接下來的操作就很簡單了:重新構(gòu)建源碼工程,得到一個(gè)程序集Community.CsharpSqlite.WP.dll(Release版本大小是525 KB)。然后,在你的WP7 Silverlight 項(xiàng)目中添加對(duì)該程序集的引用。***,你便可以使用Sqlite Client for Windows Phone提供的本地?cái)?shù)據(jù)庫支持功能了。
#p#
二、Sqlite Client for Windows Phone基礎(chǔ)類庫剖析
如果您以前有過任何基于SQL腳本的數(shù)據(jù)庫編程經(jīng)驗(yàn),那么您可以輕松地使用Sqlite Client for Windows Phone功能。特別值得一提的是,此工程建基于以前的C#-SQLite項(xiàng)目之一,通過引入幾個(gè)幫助器類(在文件SQLiteClient.cs),即SQLiteException、SQLiteConnection和SQLiteCommand,進(jìn)一步簡化了基本的數(shù)據(jù)庫和表相關(guān)操作。接下來的幾幅圖展示了Sqlite Client for Windows Phone中提供的主要組件及其之間的關(guān)聯(lián)關(guān)系。
▲圖3. Sqlite Client for Windows Phone最頂層組件
▲圖4. SQLiteConnection類中的主要組件
▲圖5. SQLiteCommand類中的主要組件
怎么樣!如果您以前熟悉任何基于SQL的數(shù)據(jù)庫開發(fā),相信上面的這些組件對(duì)您會(huì)非常親切吧。
先別急,在正式使用Sqlite Client for Windows Phone之前,有必要再向您介紹另外一個(gè)非常有用的工具,名為sqlite-manager (http://code.google.com/p/sqlite-manager/)。這個(gè)工具是以FireFox插件的方式提供的。到現(xiàn)在您應(yīng)該明白了,絕大多數(shù)與SQLite數(shù)據(jù)庫相關(guān)的操作,例如創(chuàng)建SQLite數(shù)據(jù)庫、表、視圖、索引等等,都可以通過sqlite-manager輕松完成。
1.使用SQLite Manager簡化數(shù)據(jù)庫管理
如前所述,SQLite Manager是一個(gè)Firefox插件,使用Firefox的加載項(xiàng)管理器你可以很容易地獲取和安裝這個(gè)控件(圖6)。
▲圖6. 使用Firefox的加載項(xiàng)管理器獲取和安裝SQLite Manager插件
如圖所示,如果你打開Firefox的插件管理器,然后輸入“SQLite Manager”搜索文本,你會(huì)很容易檢索到此加載項(xiàng)。然后,您可以點(diǎn)擊按鈕“添加到Firefox... ”開始下載并安裝SQLite Manager。請(qǐng)注意,在Firefox的提示后,你應(yīng)該重新啟動(dòng)Firefox以完成安裝。
使用SQLite Manager是容易的。說實(shí)在的,這是我***次使用SQLite Manager,我發(fā)現(xiàn)這個(gè)工具功能強(qiáng)大而且極易上手。如果您使用過VB6中,你可能熟悉其中內(nèi)置的數(shù)據(jù)庫管理工具-VisData(以便以內(nèi)置方式創(chuàng)建小型的Access數(shù)據(jù)庫)。說實(shí)話,VisData確實(shí)是不容易使用,但在當(dāng)時(shí)我們覺得已經(jīng)相當(dāng)不錯(cuò)了。現(xiàn)在,你只須記住,SQLite Manager的功能要比VisData強(qiáng)大1000倍。下圖展示了SQLite Manager的一個(gè)使用快照。
▲圖7. SQLite Manager使用快照
從圖中可見,你可以使用SQLite Manager來實(shí)現(xiàn)幾種各種SQLite相關(guān)操作。請(qǐng)注意,要想了解更多的有關(guān)于SQLite的概念及詳細(xì)使用語法,請(qǐng)從這款插件的幫助菜單中尋找答案。
另外,你也會(huì)注意到,在本文簡單的示例工程中,我僅創(chuàng)建了一個(gè)表格Customer,對(duì)應(yīng)的數(shù)據(jù)庫文件為database1.sqlite(如圖8所示)。
▲圖8. 使用SQLite Manager定義表格Customer字段
在創(chuàng)建完數(shù)據(jù)庫database1.sqlite后,關(guān)閉SQLite Manager插件。然后,把此文件復(fù)制或移動(dòng)到對(duì)應(yīng)示例工程WP7SQLiteClient的根目錄下。請(qǐng)注意,接下來,把它的Build Action屬性設(shè)置為Resource模式。設(shè)置成這種模式的原因與接下來的操作方式相關(guān)。如果選擇Content模式,則在后臺(tái)的示例工程中你需要修改對(duì)應(yīng)的部分源碼。
2.一個(gè)有用的工具類-DBHelper
如上所述,Sqlite Client for Windows Phone使用眾所周知的SQL操作針對(duì)典型的數(shù)據(jù)庫操作提供了一個(gè)高層次的封裝。因此,在Silverlight for Windows Phone 7編程中為了處理SQLite數(shù)據(jù)庫操作,我們可以直接使用在文件SQLiteClient.cs中定義的對(duì)象(在源庫項(xiàng)目),即SQLiteException、SQLiteConnection和SQLiteCommand等。
雖然Sqlite Client for Windows Phone并沒有提供與獨(dú)立存儲(chǔ)的直接互動(dòng),但顯然增加對(duì)獨(dú)立存儲(chǔ)支持是必要的,這樣可以改善系統(tǒng)的性能。因此,我們可以進(jìn)一步封裝前面提到的SQLiteClient對(duì)象。為此,Chris開發(fā)了一個(gè)非常好用的實(shí)用工具類,叫做DBHelper。為了應(yīng)用于我們自己的示例,我對(duì)它做了輕微的修改。完整的源碼如下。
#p#
列表1:更新版本的工具類DBHelper
- //others omitted…
- using SQLiteClient;
- using System.Linq;
- using System.IO.IsolatedStorage;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- namespace WP7SQLiteClient.Helpers
- {
- public class DBHelper
- {
- private String _dbName;
- private SQLiteConnection db = null;
- public DBHelper(String assemblyName, String dbName)
- {
- IsolatedStorageFile store =IsolatedStorageFile.GetUserStoreForApplication();
- if (!store.FileExists(dbName))
- {
- CopyFromContentToStorage(assemblyName, dbName);
- }
- _dbName = dbName;
- }
- ~DBHelper()
- {
- Close();
- }
- private void Open()
- {
- if (db == null)
- {
- db = new SQLiteConnection(_dbName);
- db.Open();
- }
- }
- private void Close()
- {
- if (db != null)
- {
- db.Dispose();
- db = null;
- }
- }
- //Insert operation
- public int Insert(T obj, string statement) where T : new()
- {
- try
- {
- Open();
- SQLiteCommand cmd = db.CreateCommand(statement);
- int rec = cmd.ExecuteNonQuery(obj);
- return rec;
- }
- catch (SQLiteException ex)
- {
- System.Diagnostics.Debug.WriteLine("Insert failed: " + ex.Message);
- throw ex;
- }
- }
- // Delete operation
- public void Delete(string statement) where T : new()
- {
- try
- {
- Open();
- SQLiteCommand cmd = db.CreateCommand(statement);
- cmd.ExecuteNonQuery();
- }
- catch (SQLiteException ex)
- {
- System.Diagnostics.Debug.WriteLine("Deletion failed: " + ex.Message);
- throw ex;
- }
- }
- //Query operation
- public List SelectList(String statement) where T : new()
- {
- Open();
- SQLiteCommand cmd = db.CreateCommand(statement);
- var lst = cmd.ExecuteQuery();
- return lst.ToList();
- }
- public ObservableCollection SelectObservableCollection(String statement)
- where T : new()
- {
- List lst = SelectList(statement);
- ObservableCollection oc = new ObservableCollection();
- foreach (T item in lst)
- {
- oc.Add(item);
- }
- return oc;
- }
- private void CopyFromContentToStorage(String assemblyName,String dbName)
- {
- IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
- System.IO.Stream src = Application.GetResourceStream(
- new Uri("/" + assemblyName + ";component/" + dbName,UriKind.Relative)).Stream;
- IsolatedStorageFileStream dest = new IsolatedStorageFileStream(dbName,
- System.IO.FileMode.OpenOrCreate,
- System.IO.FileAccess.Write, store);
- src.Position = 0;
- CopyStream(src, dest);
- dest.Flush();
- dest.Close();
- src.Close();
- dest.Dispose();
- }
- private static void CopyStream(System.IO.Stream input,IsolatedStorageFileStream output)
- {
- byte[] buffer = new byte[32768];
- long TempPos = input.Position;
- int readCount;
- do
- {
- readCount = input.Read(buffer, 0, buffer.Length);
- if (readCount > 0)
- {
- output.Write(buffer, 0, readCount);
- }
- } while (readCount > 0);
- input.Position = TempPos;
- }
- }
- }
順便說一句,對(duì)于上面這個(gè)幫助類我也沒有提供細(xì)致的優(yōu)化編碼。希望讀者根據(jù)您的相關(guān)工作能夠繼續(xù)進(jìn)行這項(xiàng)工作(例如提供更好的泛型化的CRUD支持)并分享給廣大網(wǎng)友。簡言之,我主要是增加了插入和刪除方法。上面的代碼中最引起您注意是地方一定是方法CopyFromContentToStorage,正是借助這個(gè)方法我們實(shí)現(xiàn)了上述目標(biāo)-建立起SQLite數(shù)據(jù)庫與獨(dú)立存儲(chǔ)的關(guān)系。
三、小結(jié)
本文中簡要介紹了Sqlite Client for Windows Phone的主要功能及相關(guān)的輔助開發(fā)工具。在接下來的第二篇文章中,我們將具體構(gòu)建一個(gè)簡單的Windows Phone 7客戶端應(yīng)用程序,當(dāng)然要涉及到Sqlite Client for Windows Phone的基本編程技巧。
【編輯推薦】