SQLite是一個輕量級的數據庫,廣泛應用於嵌入式和移動應用的開發上,最近在做本地數據庫的項目,記錄一下SQLite的學習筆記
首先SQLite 是一個開源的數據庫,也有很多可視化的管理軟件,大多數都是免費的,Sqlite Admin,SqliteMan,Sqlite Studio,還有一個Sqlite dotnet項目,支持在VS2010操作Sqlite數據庫(與SQL server的操作類似),下面演示這種方法
用數據庫管理軟件生成好數據庫,導入到WindowsPhone中使用
1、下載Sqlite dotnet: http://sourceforge.net/projects/sqlite-dotnet2/
2、下載完安裝包()后,安裝,安裝過程會提示Designer Installation,把VS2010打勾
也可以在安裝目錄下重新安裝 %Program Files%/SQLite.NET\bin\Designer
接下來就可以在VS2012上使用了
3、打開【服務器資源管理器】,右鍵【數據連接】,【添加連接】,【更改】選擇【SQLite Database File】,然后選擇數據庫路徑,確定
4、接着創建表,錄入數據
5、關閉,得到一個數據庫文件(MyDb.sqlite),接下來導入到WP7項目中
二、然后導入到WindowsPhone中使用
1、創建工程,導入數據庫文件(MyDb.sqlite),接下來導入到WP7項目中
Windows Phone上的Sqlite庫在Codeplex上下載 http://sqlitewindowsphone.codeplex.com/
注意:這里如果是外部導入的數據庫的話,需要把庫文件也導入進來(刪除后會出錯,原因不明),放在SQLiteClient文件夾下
2、新建一個幫助類SQLiteHelper
public class SQLiteHelper { string dbName; SQLiteConnection dbConn = null; public SQLiteHelper(string dbName) { this.dbName = dbName; IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); if (!store.FileExists(dbName)) { CopyDbFileToStorage(dbName); } } ~SQLiteHelper() { Close(); } public int Insert<T>(T obj, string sqlcmd) where T : new() { try { Open(); SQLiteCommand cmd = dbConn.CreateCommand(sqlcmd); int rec = cmd.ExecuteNonQuery(obj); return rec; } catch (SQLiteException ex) { System.Diagnostics.Debug.WriteLine("Insert failed: " + ex.Message); throw ex; } } public int Delete(string sqlcmd) { try { Open(); SQLiteCommand cmd = dbConn.CreateCommand(sqlcmd); cmd.ExecuteNonQuery(); } catch (SQLiteException ex) { System.Diagnostics.Debug.WriteLine("Deletion failed: " + ex.Message); throw ex; } } public List<T> SelectList<T>(String sqlcmd) where T : new() { try { Open(); SQLiteCommand cmd = dbConn.CreateCommand(sqlcmd); var lst = cmd.ExecuteQuery<T>(); return lst.ToList<T>(); } catch (SQLiteException ex) { System.Diagnostics.Debug.WriteLine("Select Failed: " + ex.Message); throw ex; } } private void Open() { if (dbConn == null) { dbConn = new SQLiteConnection(dbName); dbConn.Open(); } } private void Close() { if (dbConn != null) { dbConn.Dispose(); dbConn = null; } } /// <summary> /// 從資源文件中賦值到隔離存儲空間 /// </summary> /// <param name="dbName"></param> public void CopyDbFileToStorage(string dbName) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { Uri uri = new Uri(dbName, UriKind.Relative); using (var src = Application.GetResourceStream(uri).Stream) { using (var dest = new IsolatedStorageFileStream(dbName, FileMode.OpenOrCreate, FileAccess.Write, store)) { src.CopyTo(dest); } } } } }在定義一個People類
public class People { public int Id { get; set; } public string Name { get; set; } public int Age { get; set;} }3、接下來,查詢,插入,刪除操作,支持部分SQL語句,基本都是通過SQL語句來實現對數據庫的操作
public void TestQuery() { SQLiteHelper helper = new SQLiteHelper("MyDb.sqlite"); List<People> list = helper.SelectList<People>("SELECT * FROM People"); } public void TestInsert() { People people = new People { Id = 3, Name = "tobi", Age = 21 }; SQLiteHelper helper = new SQLiteHelper("MyDb.sqlite"); string sqlcmd = "Insert into People (Id,Name,Age) values (@Id,@Name,@Age)"; //返回受影響數目 int rec = helper.Insert<People>(people, sqlcmd); } public void TestDelete() { SQLiteHelper helper = new SQLiteHelper("MyDb.sqlite"); string sqlcmd = "Delete from People where Id=1"; int rec = helper.Delete(sqlcmd); }