Quantcast
Channel: Category Name
Viewing all articles
Browse latest Browse all 10804

Using the SQLite database engine with Windows Phone 8 apps

$
0
0

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:

  1. Add the sqlite-net package (1.0.7) to our Windows Phone app project:
    • Open the NuGet library Package Manager Console:
      clip_image006
    • 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.
  2. 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:
    • 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.
    • 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.
        clip_image008

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 in the GitHub wiki. The information referenced in this link talks about Windows Store apps, but it is equally applicable to Windows Phone apps once you have completed the steps above.


Viewing all articles
Browse latest Browse all 10804

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>