Tag Archives: UWP

Working with SQLite in UWP

*Post will be updated with explanation soon!*

The following samples have been tested on Desktop, Mobile and IoT (Raspberry Pi 2). They require that you have the 10586 SDK installed (Visual Studio 2015 Update 1 or later).

UWP using Entity Framework 7 – use SQLite for persistent storage: EF7Sample

UWP using SQLite-NET, second example using SQLite-WinRT NuGet.
The SQLite-NET sample also demonstrates accessing a SQLite database from a background task (Click the Background Task link on the command bar menu to navigate to a page where you can register and signal (i.e. start) the background task).
*Check the SQLite Extension SDK references in the foreground projects. The samples were built with 3.10.0 but if you have a different SQLite version installed, you will need to remove and then re-add the SQLite extension reference*
SQLiteSamplesUWP

Using SQLite-WinRT in UWP apps

There are a number of SQLite wrapper APIs out there. In the recent MVA A Developer’s Guide to Windows 10, in module 10  I covered SQLite Local Database and walked through an example of using the SQLite-PCL wrapper from MSOpenTech. There are many other wrappers out there, the most popular of which (and which offers a very nice LINQ API) is SQLite-NET.

However, the one I support is called SQLite-WinRT which has a small but faithful band of followers, and I’ve been getting some queries about using it in Windows 10 UWP apps. For those who haven’t used it, SQLite-WinRT is a lightweight wrapper around the SQLite ‘C’ API and so operates using SQL statements – there are a number of other posts on this blog talking about it such as this one: SQLite-WinRT: Database programming on Windows Phone and Windows 8

So I settled down today to make sure it worked on UWP. Now most of the time, libraries written for Windows 8.1 Store apps or for Windows Phone 8.1 (WinRT) just work in UWP apps – but not this time. For some reason, when you try to new up an instance of the SQLiteWinRT Database object, we get a FileNotFoundException. That just seems a bit weird, and in these pre-RTM times, I’m not going to burn loads of hours investigating it. So instead, I built a new WinRT component specifically targeting Windows 10 UWP and tested it out – that works!

However, after some hours of trying things out, it appears that currently there’s no way of building a NuGet package that contains unique libraries for Windows 8.1/Windows Phone 8.1 *and* for UWP. That support will come with an update to NuGet as we get nearer to RTM.

So for now, to use SQLite-WinRT in a UWP app you will have to add the project for the wrapper API to your solution. You can download a sample project with the SQLite-WinRT component source included here:

Also, don’t forget to go to Tools – Extensions and Updates in Visual Studio 2015, click Online in the left panel and then search for and install SQLite for Universal  App Platform. Then in your client project, add a reference to SQLite for Universal App Platform from the Add Reference wizard:

AddRef

Don’t forget, if your app is targeting Windows 8.0/8.1 or Windows Phone 8.1 (WinRT) or 8.0/8.1(Silverlight) you can still just use the existing SQLite-WinRT NuGet package.