Using SQLite in Windows 10 Universal apps

Using SQLite in Windows 10 Universal apps

Using SQLite in Windows 10 Universal apps (UWP) is really easy even in this preview phase. Even though Entity Framework 7 support for Windows 10 Universal apps is almost here, you still might decide to just continue using a lighter SQLite.Net-PCL library that you're used to since Windows Phone 8/WinRT.

Note that that everything here is written using Windows 10 Technical Preview build 10074 and Technical Preview Tools version 10.0.10069.0, so things can change. Also, SQLite extension is in pre-release, so things can break, stop working or change. EDIT: Post has been updated with the latest SQLite VSIX package. The rest should work in 10240.

Luckily, if you want to, you can.

There's already a pre-release version of SQLite Visual Studio extension for Visual Studio 2015 RC that you can download from SQLite.org.

Again, this is pre-release, but it seems to work with Visual Studio 2015 RC, too.

EDIT: If you are using Visual Studio 2015 RTM and 10240 SDK, there's now the official (no more pre-release stuff) SQLite VSIX package that you can download from SQLite.org. Search for "Universal App Platform" and you're good to go! The rest of the blog post should apply to this version as well.

Official SQLite VSIX package for Windows 10 UAP

The next step is to add the SQLite.Net-PCL library that I already mentioned. Use the (redesigned) NuGet to do that.

NuGet package for SQLite.Net-PCL library

It will add two references

  • SQLite.Net
  • SQLite.Net.Platform.WinRT

Now, remember the VSIX package (Visual Studio extension) installed earlier? It installs SQLite extensions that you need to reference by simply right-clicking on References and choosing "Add Reference..." and then finding the right reference under Windows Universal -> Extensions.

Add SQLite reference extension

Notice that two extensions are selected. Even if you try adding only the SQLite for Universal App Platform, the dependency called Microsoft Visual C++ AppLocal Runtime Package for Windows UAP will be added automatically.

And that's it! You can start using SQLite!

To test it, I defined a simple model class called User.

public class User  
{
    public int Id { get; set; }
    public string Name { get; set; }
}

And then I created a table which will hold User entities.

var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");

using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))  
{
    conn.CreateTable<User>();
}

And that's all!

Conclusion

Adding SQLite support to Windows 10 Universal apps is really simple! After all of the extensions are added, and the ORM/client library fetched over NuGet, the usage is the same as before. This is really neat for simpler scenarios and until Entity Framework 7 officially, fully supports Windows 10 Universal apps.

Igor Ralic

igor ralic

View Comments
Microsoft Certified Solutions Developer: Windows Store Apps in C#