【WP7】Sqlite使用
用了几天Sqlite数据库写WP7项目,在这里做下笔记
http://wp7sqlite.codeplex.com/
之前看到还有一个封装(http://sqlitewindowsphone.codeplex.com/)但感觉用上面这个比较好
下载项目,可以得到两个库文件
Community.CsharpSqlite.WP7.dll
Community.CsharpSqlite.SqlLiteClient.WP7.dll
编写下面类对其进行再封装
public static class SqliteHelper { private static string DbName; private static SqliteConnection _conn; static SqliteHelper() { if (DbName == null) throw new Exception("请先对DbName赋值"); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!store.FileExists(DbName)) { CopyDbFileToStorage(DbName); } } } public static int ExecuteNonQuery(string sql, SqliteParameter[] parameters = null) { Open(); var res = 0; using (var command = _conn.CreateCommand()) { command.CommandText = sql; if (parameters != null) foreach (var sqliteParameter in parameters) command.Parameters.Add(sqliteParameter); res = command.ExecuteNonQuery(); } return res; } public static List<T> ExecuteQuery<T>(string sql, SqliteParameter[] parameters = null) where T : new() { Open(); var list = new List<T>(); using (var command = _conn.CreateCommand()) { command.CommandText = sql; if (parameters != null) foreach (var sqliteParameter in parameters) command.Parameters.Add(sqliteParameter); using (var reader = command.ExecuteReader()) { while (reader.Read()) { var a = new T(); //获取所有行 var colcount = reader.FieldCount; for (var i = 0; i < colcount; i++) { var propertyname = reader.GetName(i); if (reader.IsDBNull(i)) continue; var peopertyvalue = reader.GetValue(i); a.GetType().InvokeMember(propertyname, BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance, null, a, new object[] { peopertyvalue }); } list.Add(a); } } } return list; } //查询单个值(比如查询表中的行数) public static T ExcuteQuery<T>(string sql, SqliteParameter[] parameters = null) { T obj; Open(); using (var command = _conn.CreateCommand()) { command.CommandText = sql; if (parameters != null) foreach (var sqliteParameter in parameters) command.Parameters.Add(sqliteParameter); using (var reader = command.ExecuteReader()) { while (reader.Read()) { obj = reader.GetValue(0); } } } return obj; } private static void Open() { if (_conn != null) return; _conn = new SqliteConnection("Version=3,uri=file:" + DbName); _conn.Open(); } private static void CopyDbFileToStorage(string dbName) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { Uri uri = new Uri("Data/" + dbName, UriKind.Relative); using (var src = Application.GetResourceStream(uri).Stream) { using (var dest = store.CreateFile(dbName)) { src.CopyTo(dest); } } } } }
使用:
public void test() { //插入 var sqlcmd = "INSERT INTO MyPoem(Title, Content) values(@Title,@Content)"; var paras = new[] { new SqliteParameter("@Title", tbTitle.Text), new SqliteParameter("@Content", tbContent.Text) }; int res = SqliteHelper.ExecuteNonQuery(sqlcmd, paras); //查询行数 sqlcmd = "SELECT COUNT(*) FROM Poem"; var count = Convert.ToInt32(SqliteHelper.ExcuteQuery<long>(sqlcmd)); //查询 sqlcmd = "SELECT * FROM Poem"; var list = SqliteHelper.ExecuteQuery<PoemItem>(sqlcmd); //改 sqlcmd = "UPDATE Poem SET Title=@Title, Content=@Content WHERE Id=@Id"; var paras = new[] { new SqliteParameter("@Title", tbTitle.Text), new SqliteParameter("@Content", tbContent.Text), new SqliteParameter("@Id", _poem.Id) }; res = SqliteHelper.ExecuteNonQuery(sqlcmd, paras); //删除 const string sqlcmds = "DELETE FROM Recent WHERE Id BETWEEN 20 AND 40"; res = SqliteHelper.ExecuteNonQuery(sqlcmds); }
SQLite的一些使用
1、随机读取:SELECT * FROM 表名 ORDER BY RANDOM() LIMIT 1
测试发现,当数据库的数据量比较大的时候,用这种方式读取效率比较低,用Random生成主键,然后根据主键查询会快一些
SELECT COUNT(*) FROM 表名
查到行的总数count,然后
Random ran = new Random();
int id = ran.Next(count);
然后根据Id来查,其中,Id为主键
SELECT * FROM 表名 WHERE Id=@Id
这种方式查询速度更快些
2、自动增长字段:通过 AUTOINCREMENT 声明
3、获取最后一次插入的行
SELECT last_insert_rowid() FROM 表名
4、使用替换插入
REPLACE INTO 表(列名) VALUES(值)
如果主键已经存在,则替换为新的值,否则直接插入
5、读取数目和位置
SELECT * FROM 表名 LIMIT 10 OFFSET 20
从第20个开始读取10行
6、SQLite在删除数据的时候,不会对占用的空间进行回收,数据库的大小不会变小,通过下面语句缩减大小
VACUUM [index-or-table-name]
7、查询前判断是否已经存在
INSERT INTO People(id, name) SELECT ?,? WHERE NO EXISTS
(SELECT 1 FROM People WHERE id = 1)