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.

4 thoughts on “Using SQLite-WinRT in UWP apps

  1. Alex

    I was able to figure it out. I was using the wrong SQLite for Windows Runtime package. It is not the one redistributed by author v. leschenko but a .vsix package from the SQLite website.

    Reply
    1. admin Post author

      That’s right. You can also install it just by going to the Visual Studio Tools menu, option Updates and Extensions, then select the Online category and search for SQLite.

      Reply
  2. Ben

    Got the same FileNotFoundException , and already found at that it is actually missing sqlite3.dll.
    And so far I have been unable to add that file as a reference (the ARM version, because it causes a COM error when you try to add it)
    And trying your source did not work either as you probably did this in the pre RTM version, and those will not work in the released version.

    So if you would have any (portable) class library using SQLite on Windows IOT (Raspberry PI) Maybe you could send me a copy.

    Happy New Year!
    Thanks

    Ben

    Reply
    1. admin Post author

      For anyone still getting a File Not Found exception for sqlite3.dll- particularly on IoT or Mobile devices – make sure you not only add ref to the SQLite for Universal Windows Platform extension (note name change from previous: was SQLite for Universal App Platform), but also make sure you add ref to the Visual C++ 2015 Runtime for Universal Windows Apps. If you do not add ref to the latter, you will see this File not Found error.
      It’s a confusing error – the Visual C++ runtime is a shared runtime component so if you already have another app installed that references it, it will already be on your target so you don’t see any problems (as is usually the case with deploying to a desktop target). But for Mobile or IoT, that shared runtime component may not already be there, so when it tries to use sqlite3.dll, it fails, but confusingly reports it as a FNF.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *