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); }