MVP可以在channel 9上传视频了,所以准备做个英文视频传上去分享给大家,本文做稿子。

Hello everyone,

As we all know, SQLite is a great and popular local database running on mobile devices. Consider of its powerful and useful features, lots of apps use SQLite database as the main way of application data storage, including Android and iOS apps. What's more, SQLite is a cross-platform database and also have windows version so that windows apps are easy to integrate with it. Today I will give a lesson about using SQLite in UWP project.

Install

Firstly, it's necessary for us to install SQLite for windows. you can download the binary install file here. Actually, you can alse download the source and build it by youself, if you like source more. After you finnished installation, you can find the sqlite extension in the refrence window. It will look like this:

And we need to add the VC++ 2015 Runtime refrence too.

 Project Configuration

Secondly, we need to add a framework named SQLite.Net for using SQLite effectively. In other words, SQLite.Net libary will help us access sqlite database more esaily. It have some relese versions which you can find in NuGet, and two of them are most useful, including SQLite.Net-PCL and SQLite.Net.Async-PCL.

What's the defference between SQLite.Net-PCL and SQLite.Net.Async-PCL framework is that SQLite.Net.Async-PCL support asynchronous operations. Actually I like async-await more, so I will install SQLite.Net.Async-PCL framework. Once we finish configurations, we can write some code to use SQLite now.

SQLiteDBManager

Let's we have some interesting codes to begin using this amazing tool now. What's more, I will provide some example codes written by myself for you. The mainly code is used to manager local SQLite database file and access it more easily, so I named it SQLiteDBMnager.

Before we access the data of database, we need import and manage the database file. In windows runtime framework we need to move or create database file in ApplicationData folder. And you can create a database file using SQLite Expert which is famous manage tool for SQLite database.

/// <summary>/// init db /// </summary>private static async void InitDBAsync(){try{var file = await ApplicationData.Current.LocalFolder.TryGetItemAsync("ysy.sqlite");if (file == null){var dbFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Data/ysy.sqlite"));file = await dbFile.CopyAsync(ApplicationData.Current.LocalFolder);var dbConnect = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), new SQLiteConnectionString(file.Path, true)));//create db tablesvar result = await dbConnect.CreateTablesAsync(new Type[] { typeof(Product), typeof(P2PData), typeof(ProductDetail), typeof(P2PDataDetail), typeof(ProductExtend), typeof(P2PDataExtend) });Debug.WriteLine(result);}}catch (Exception ex){Debug.WriteLine(ex.Message);}}

After we initialize database, we need to access the data of database. Fortunately, SQLite.NET provides some method for us, but we still do some works to simplify codes.

/// <summary>/// get current DBConnection/// </summary>/// <returns></returns>public async Task<SQLiteAsyncConnection> GetDbConnectionAsync(){if (dbConnection == null){var path = await GetDBPathAsync();dbConnection = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), new SQLiteConnectionString(path, true)));}return dbConnection;}

Add/Delete/Modify/Find

/// <summary>/// insert a item /// </summary>/// <param name="item">item</param>/// <returns></returns>public async Task<int> InsertAsync(object item){try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.InsertOrReplaceAsync(item);}catch (Exception ex){Debug.WriteLine(ex.Message);return -1;}}/// <summary>/// insert lots of items/// </summary>/// <param name="items">items</param>/// <returns></returns>public async Task<int> InsertAsync(IEnumerable items){try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.InsertOrReplaceAllAsync(items);}catch (Exception ex){Debug.WriteLine(ex.Message);return -1;}}/// <summary>/// find a item in database/// </summary>/// <typeparam name="T">type of item</typeparam>/// <param name="pk">item</param>/// <returns></returns>public async Task<T> FindAsync<T>(T pk) where T : class{try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.FindAsync<T>(pk);}catch (Exception ex){Debug.WriteLine(ex.Message);return null;}}/// <summary>/// find a collection of items/// </summary>/// <typeparam name="T">type of item</typeparam>/// <param name="sql">sql command</param>/// <param name="parameters">sql command parameters</param>/// <returns></returns>public async Task<List<T>> FindAsync<T>(string sql, object[] parameters) where T : class{try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.QueryAsync<T>(sql, parameters);}catch (Exception ex){Debug.WriteLine(ex.Message);return null;}}/// <summary>/// update item in table /// </summary>/// <typeparam name="T">type of item</typeparam>/// <param name="item">item</param>/// <returns></returns>public async Task<int> UpdateAsync<T>(T item) where T : class{try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.UpdateAsync(item);}catch (Exception ex){Debug.WriteLine(ex.Message);return -1;}}/// <summary>/// update lots of items in table/// </summary>/// <typeparam name="T">type of item</typeparam>/// <param name="items">items</param>/// <returns></returns>public async Task<int> UpdateAsync<T>(IEnumerable items) where T : class{try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.UpdateAllAsync(items);}catch (Exception ex){Debug.WriteLine(ex.Message);return -1;}}/// <summary>/// delete data from table/// </summary>/// <typeparam name="T">type of item</typeparam>/// <param name="item">item</param>/// <returns></returns>public async Task<int> DeleteAsync<T>(T item) where T : class{try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.DeleteAsync<T>(item);}catch (Exception ex){Debug.WriteLine(ex.Message);return -1;}}/// <summary>/// delete all items in table/// </summary>/// <param name="t">type of item</param>/// <returns></returns>public async Task<int> DeleteAsync(Type t){try{var dbConnect = await GetDbConnectionAsync();return await dbConnect.DeleteAllAsync(t);}catch (Exception ex){Debug.WriteLine(ex.Message);return -1;}}

View Code

Last but not least, we can access the database easily. For example,

 /// <summary>/// get product tabele data/// </summary>/// <returns></returns>private async Task<List<Fund>> GetFundDataFromDataBaseAsync(){var manager = SQLiteDBManager.Instance();var funds =new List<Fund>();var products = await manager.FindAsync<Product>("select * from Product", null);products.ForEach(p => {funds.Add(new Fund { Id=p.ProductId.ToString(), Host=p.Company, Image=p.Icon, Name=p.Name, Platform=p.FoundationName, QRNH=p.QRNH, WFSY=p.WFSY });});if (funds != null && funds.Count > 0){return funds;}return null;}

You can get entire file here.

Conclusion

SQLite database is a good choice as application data storage container, and more excellent that ApplicationSettings and Xml/Json data files in many ways, I think.

Using SQLite database in your Windows 10 apps相关推荐

  1. 如何在Windows 10上管理应用程序权限

    Modern Windows 10 apps have permissions you can control, just like modern iPhone, iPad, and Android ...

  2. Building Apps for Windows 10 on LattePanda–Jump Start

    1.引言 目前来看,LattePanda应该是最小的运行Full Windows 10系统的开发板了(注意,不是Windows 10 for Mobile,也不是Windows 10 IoT系列,而是 ...

  3. svn注册表编辑器怎么打开_如何在Windows 10上打开注册表编辑器

    svn注册表编辑器怎么打开 Windows and a lot of third-party applications store their settings in the registry. Th ...

  4. windows1064安装oracle,在Windows 10系统下安装Oracle 11g数据库

    由于最近想深入的学习一下oracle所以.跑着虚拟机在跑数据库比较卡.比较难受.所以经过仔细认真研究终于在windows 10上安装成功了. 准备工作: 直接将两个压缩包加压,解压之后变为一个文件夹. ...

  5. What is the purpose for IT Pro in Windows 10 Creators Update

    Windows 10, version 1703-also known as the Windows 10 Creators Update-is designed for today's modern ...

  6. Windows 10 安全移动 MySQL 数据库的存储位置

    系统: Windows 10 64bit 数据库: MySQL 5.7 要修改数据库存储的位置,比如从默认安装目录下的C:\ProgramData\MySQL\MySQL Server 5.7\Dat ...

  7. Windows 10如何启用ReFS弹性文件系统

    责任编辑:editor005 |  2016-10-10 14:26:56 本文摘自:51CTO 自微软为 Windows 操作系统定制和设计 NTFS 文件系统之后,用户的数据存储需求还是发生的巨大 ...

  8. Windows 10封装中出现“无法验证你的Windows安装”错误解决方法

    近期(当时写文章时在今年4月份)在测试在Windows Server 2016 的虚拟桌面,在测试Windows 7的虚拟桌面时一切良好,但在测试Windows 10的虚拟桌面时,在对Windows ...

  9. win10任务栏和开始菜单_如何将网站固定到Windows 10任务栏或开始菜单

    win10任务栏和开始菜单 Having quick access to frequently-used or hard to remember websites can save you time ...

最新文章

  1. 面试准备3——相关知识
  2. OpenMP在ARM-Linux以及NDK中的编译和使用
  3. mybatis注解开发使用二级缓存
  4. 【SRM-05 B】无题?
  5. 窗体的布局 1124
  6. 任天堂的好日子還會繼續嗎﹖
  7. 路由器NAT负载均衡实验过程详解
  8. 【bzoj3174】[Tjoi2013]拯救小矮人 贪心+dp
  9. 对于(不是特别不合理)的指摘、的对应方式(学会调整,不要一根筋)
  10. 去重 属性_Javascript算法 — 数组去重
  11. few-shot learning, zero-shot learning, one-shot learning,any-shot learning, C-way K-shot,Meta-learn
  12. 计算原矩阵Jaccard相似度和签名矩阵的Jaccard相似度
  13. nginx过滤HttpHeader的 中划线
  14. 敬天爱人 大道至简——初读《经营十二条》
  15. 怎么做读书分享PPT课件?
  16. c语言性能测试库,C语言qsort函数算法性能测试
  17. 计算机硬件信息被修改怎么还原,修改bios硬件信息方法
  18. Linux 下安装应用程序
  19. wget命令 scp命令 rcp命令
  20. ip地址掩码和位数对应关系表、子网掩码、网络地址、主机地址-yellowcong

热门文章

  1. 21天Jmeter打卡Day21生成HTML测试报告
  2. 《深入理解Java虚拟机》第5章 调优案例分析与实战
  3. linux中板子烧写环境配置,3、在Linux下搭建51单片机的开发烧写环境(makefile版)...
  4. rabbitmq接收不到消息_分布式消息队列:如何保证消息的可靠性传输
  5. php 扩展库 开发环境,PHP 扩展开发环境搭建
  6. php 隐藏路径,急!!!隐藏路径问题
  7. YOLOv3: An Incremental Improvement
  8. cnn风格迁移_愚蠢的CNN,换个马甲就认不出猫!但,这病能治 | ICLR Oral
  9. 百度图神经网络学习——day05:图神经网络进阶模型
  10. html css字幕滚动代码,纯CSS实现滚动3D字幕