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
- Open Visual Studio and select Tools > Extensions and Updates.
- Select Online from the left pane.
- Search for “SQLite for Windows Phone” in the search box in the top-right corner of the Extensions and Updates dialog box.
- Locate the SQLite for Windows Phone package.
- 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
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.
- Open the NuGet library Package Manager Console:
- Run the following command to install the NuGet package to your project:
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:
- Go to https://github.com/peterhuene/sqlite-net-wp8.
- Download the zip version of the repository.
- Right-click the zip, click Properties, and then click the Unblock button.
- Unzip the content.
- Right-click your Solution and choose Add > Existing Project and select the
Sqlite.vcxproj that you have just downloaded and unzipped.
- 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:
- Right-click the project and select Properties.
- Select the Build tab.
- In the Configuration list, select All Configurations.
- In the Platform list, select All Platforms.
- 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.
- 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.