sqlite是一种轻量级的数据库,对于一些资源紧张又需要数据库的开发非常好用。
SQLite 是一个开源的无服务器嵌入式数据库。 这些年来,它已作为面向存储在许多平台和设备上的数据的主要设备端技术出现。 通用 Windows 平台 (UWP) 支持并建议使用 SQLite 实现跨所有 Windows 10 设备系列的本地存储。
SQLite 最适用于手机应用、面向 Windows 10 IoT 核心版(IoT 核心版)的嵌入式应用程序,以及作为企业关系数据库服务器 (RDBS) 数据的缓存。 它将满足大多数本地数据访问需求,除非这些数据需要大量的并发写入,或除了与大多数应用不同的大数据规模方案。
在媒体播放和游戏应用程序中,SQLite 还可用作文件格式来存储目录或其他资源(例如游戏级别),可以从 Web 服务器按原样下载该文件格式。
这里是微软的官方文档:https://msdn.microsoft.com/zh-cn/windows/uwp/data-access/sqlite-databases
1.首先,下载和安装sqlite,这里是下载地址:http://sqlite.org/download.html
对于UWP开发,应该下载Universal Windows Platform版本,就是这个:
下载完成后,安装即可。
2.接着添加对sqlite的引用:
在解决方案中,右击引用-》添加引用-》引用管理器中,打开Universal Windows标签页-》选中SQLite for Universal Windows Platform,确定。
如果这里没有SQLite for Universal Windows Platform,可能版本不对,也可能你没有正确执行第一步。
3.如果你想使用C#开发,当然还要添加SQLitePCL引用。右击引用-》管理Nuget程序包-》在“浏览”标签页下搜索SQLite.Net-PCL,安装SQLitePCL。
安装完成后,你的引用下面应该是类似这样的,注意,一定有红线画的两个引用。
4.现在,你可以开发了。
新建一个类,就Book.cs吧,我这里只是个示例,因为后面用到一下
public class Book { [PrimaryKey,AutoIncrement] public int Id { get; set; } [MaxLength(64)] public string Name { get; set; } public string Description { get; set; } }
注意添加using SQLite.Net.Attributes;引用
上面的[PrimaryKey,AutoIncrement]是对Id的约束属性,表示Id是主键且自增;[MaxLength(64)]则是表明Name最大长度为64。不懂的谷歌一下,属于SQL的基础内容。
对数据库操作不外乎增删改查,这里提供一个工具类供参考。
using SQLite.Net;using SQLite.Net.Platform.WinRT;using SqliteSample.Model;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using Windows.Storage;namespace SqliteSample{ public class Dal { private static string dbPath = string.Empty; private static string DbPath { get { if (string.IsNullOrEmpty(dbPath)) { dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Sqlite.db"); } return dbPath; } } private static SQLiteConnection DbConnection { get { return new SQLiteConnection(new SQLitePlatformWinRT(), DbPath); } } public static void CreateDatabase() { // 创建连接 using (var db = DbConnection) { //创建Book表 var c = db.CreateTable(); //插入示例数据 Book book = new Book(); book.Id = 1; book.Name = "C# in depth"; book.Description = "good book"; db.InsertOrReplace(book); } } /// /// 删除记录 /// /// 实体 public static void DeleteBook(Book book) { using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath)) { db.Execute("DELETE FROM Person WHERE Id = ?", book.Id); } } ////// 获取全部的记录 /// ///全部实体集合 public static ListGetAllPersons() { List models; using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath)) { models = (from p in db.Table () select p).ToList(); } return models; } /// /// 获取记录 /// /// 实体Id ///实体 public static Book GetBookById(int Id) { using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath)) { Book m = (from p in db.Table() where p.Id == Id select p).FirstOrDefault(); return m; } } /// /// 创建或更新一条记录 /// /// 实体 public static void SaveBook(Book book) { using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath)) { if (book.Id == 0) { // 新建 db.Insert(book); } else { // 更新 db.Update(book); } } } }}
Model就是上面的Book.cs
5.结语
sqlite还是挺好用的,除了微软提供的ApplicationData储存用户数据之外,sqlite可以储存图片什么的,不像自带的应用存储对类型有限制。
可以交月报了吗?2333考试啊,一看都31号了,都忘交了好几次月报了,再不交就对不起组织了2333,本来还有几篇,没想到好浪费时间啊,滚去复习啊