Skip to main content
March 12, 2013
Windows Phone Developer Blog

Using the SQLite database engine with Windows Phone 8 apps



This blog post was authored by Himadri Sarkar, a program manager on the Windows Phone team.

– Adam


You may have heard that with the Windows Phone SDK 8.0 release, the SQLite database engine is fully supported on the Windows Phone platform. Many thanks to the SQLite team for making the SQLite engine for Windows Phone available as a Visual Studio extension. In this post, I would like to show you how you can use the SQLite database engine from your Windows Phone 8 project.

The first step is to install the Visual Studio extension package, which contains all the required pieces.

Getting the Visual Studio extension package

  1. Open Visual Studio and select Tools > Extensions and Updates.
    clip_image002
  2. Select Online from the left pane.
  3. Search for “SQLite for Windows Phone” in the search box in the top-right corner of the Extensions and Updates dialog box.
  4. Locate the SQLite for Windows Phone package.
    clip_image004
  5. Download and install the package.
    You are all set to use SQLite in your managed or native project. The component includes all the libs, headers, and precompiled SQLite DLLs for Windows Phone emulator (x86) and device (ARM).
    Note: You may need to restart Visual Studio after package installation.

Building a quick Windows Phone sample app using SQLite

To start with a sample app, create a blank Windows Phone App project and name it SQLiteSampleApp.

Let’s add the support for SQLite database to this app.

Because the SQLite engine is a native component, using it in a Windows Phone 8 managed app requires the APIs to be wrapped in a Windows Phone Runtime component that can be called from the managed layer.

There is already a “Windows 8” Runtime component available in github for this purpose, and there is a NuGet package that you can use in your project to get this component. This sqlite-net wrapper offers a bunch of synchronous as well as asynchronous methods for easy usage of SQLite.
However the sqlite-net wrapper does not have support for the native SQLite Windows Phone 8 extension that we downloaded from the Visual Studio Gallery earlier. Instead, it relies on an open-source implementation of SQLite called csharp-sqlite.
To use the sqlite-net wrapper, we use a fork created by Peter Huene that allows use of the native SQLite Windows Phone 8 extension. This fork has recently been pulled into the sqlite-net master branch, and the latest NuGet package (1.0.7 at the time of writing) also reflects this update.

There are a few things we need to get done to get this all working. The following section explain how.

The sqlite-net package (1.0.7)

Start by adding the sqlite-net package (1.0.7) to the Windows Phone app project using the following steps.

  1. Open the NuGet library Package Manager Console:

    clip_image006

  2. Run the following command to install the NuGet package to your project:

    Install-package sqlite-net

    This installs the NuGet package for the sqlite-net extension, and the SQLite.cs and SQLiteAsync.cs files are added to your project.

The sqlite-net-wp8 native C++ project

Download and add the sqlite-net-wp8 native C++ project to the solution. This is the wrapper project that will replace the csharp-sqlite functionality. At this time, there’s no NuGet package available for sqlite-net-wp8, so we need to download and add it to our project manually. Use the following steps to download and add the project:

  1. Go to https://github.com/peterhuene/sqlite-net-wp8.
  2. Download the zip version of the repository.
  3. Right-click the zip, click Properties, and then click the Unblock button.
  4. Unzip the content.
  5. Right-click your Solution and choose Add > Existing Project and select the Sqlite.vcxproj that you have just downloaded and unzipped.
  6. Add a reference to the sqlite-net-wp8 native C++ project from the Windows Phone app project and add the USE_WP8_NATIVE_SQLITE compiler directive to it.

Additionally, to indicate to sqlite-net that we want to use the sqlite-net-wp8 project added in earlier steps, we need to add a compiler directive to the Windows Phone app project:

  1. Right-click the project and select Properties.
  2. Select the Build tab.
  3. In the Configuration list, select All Configurations.
  4. In the Platform list, select All Platforms.
  5. On the Build tab, you’ll see Conditional compilation symbols under the General header, containing a default value of SILVERLIGHT;WINDOWS_PHONE on a Windows Phone app project. Change the value to SILVERLIGHT;WINDOWS_PHONE;USE_WP8_NATIVE_SQLITE and save the project file.
    clip_image008
  6. Also, add the reference to this SQLite project to your Managed Project created earlier (SQLiteSampleApp).

After you have set up everything, you can test the SQLite database functionality by using the sample code referred to in the GitHub wiki. The information in this link is about Windows Store apps, but it should be helpful for Windows Phone apps after you have completed the steps in this post.