Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Using SQLite with Windows Phone 8 apps

, 15 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Using SQLite with Windows Phone 8 apps.

SQLite

We all know SQL Server can handle large data, but here we will use SQLite, it can make your Windows Phone 8 application processing data easier, now let us see how to use SQLite with your Windows Phone 8 app.

Getting the Visual Studio extension package

  1. Open Visual Studio and select Tools > Extensions and Updates.
  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.
  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:
  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 C#-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.
  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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zoyobar

United States United States
No Biography provided

Comments and Discussions

 
GeneralBroken image links PinmemberNigel-Findlater8-Sep-14 0:58 
QuestionThe type or namespace name 'Sqlite' could not be found (are you missing a using directive or an assembly reference?) Pinmember94106624223-Oct-13 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 Apr 2013
Article Copyright 2013 by zoyobar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid