Introduction

If you want to implement a SQLite database application for Windows Phone 8.0, my previous article might help. This article explains "SQLite support in Windows Phone Store 8.1". This article is compared with my previous article, so that we can easily understand the changes in WP8.0 and WP8.1 to setup SQLite environment.

SQLite

Requirements

Table of Contents

Some extensions need to be installed to interact with Sqlite from your WP8.1 app. And now it is very easy to set up the SQLite environment in our apps. So this article covers SQLite at the beginners level like this.

  1. How to set up the SQLite environment
  2. How to perform all the SQLite CRUD operations
  3. How to bind SQLite data to a listbox
  4. How to explore "SQLite" database data

Description

SQLite is a very lightweight database. In this tutorial I'll explain how to write classes to handle all the SQLite operations. So let's start with the preceding hierarchy that was previously discussed in the "Table of Contents".

1. How to set up the SQLite environment

Sqlite is not available directly in Windows Phone. Don't worry however since it is now very easy to set up SQLite in Windows Phone apps. We need to use only two steps (instal SQLite for Windows Phone SDK and instal the sqlite-net-wp8 package).

2. How to perform all the SQLite CRUD operations

Note: From this step on-ward, most of the content will be the same as in my previous article. Please note that a few changes were made in this article.

So its time to perform all the SQLite Create, Read, Update, Delete (CRUD) operations. So my thought for this sample is to make a single "DatabaseHelperClass.cs" class for the entire application and handle the SQLite operations with this helper class. Let's first see my table structure.

DatabaseHelperClass

Here I am trying to create table named "Contacts" in the "ContactsManager.sqlite" database. So my class "Contacts" has all the getter and setter methods (Id, Name, PhoneNumber, CreatedDate) to maintain a single contact as an object.

C# Code

  1. public class Contacts
  2. {
  3. //The Id property is marked as the Primary Key
  4. [SQLite.PrimaryKey, SQLite.AutoIncrement]
  5. public int Id { get; set; }
  6. public string Name { get; set; }
  7. public string PhoneNumber { get; set; }
  8. public string CreationDate { get; set; }
  9. public Contacts()
  10. {
  11. //empty constructor
  12. }
  13. public Contacts(string name, string phone_no)
  14. {
  15. Name = name;
  16. PhoneNumber = phone_no;
  17. CreationDate = DateTime.Now.ToString();
  18. }
  19. }

3. How to bind SQLite data to listbox

In the preceding Step 2.1, I created a database helper class named "DatabaseHelperClass.cs" that is the main head for this sample to perform all the SQlite operations. Let's first see my project hierarchy like this.

bind SQLite data to listbox

In the app.xaml class let's create a database. In the constructor we check if the database exists and if it does not we create it. Since if no file exists, it will get an exception.

C# Code
  1. public partial class App : Application
  2. {
  3. public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "ContactsManager.sqlite"));//DataBase Name
  4. public App()
  5. {
  6. if (!CheckFileExists("ContactsManager.sqlite").Result)
  7. {
  8. using (var db = new SQLiteConnection(DB_PATH))
  9. {
  10. db.CreateTable<Contacts>();
  11. }
  12. }
  13. }
  14. private async Task<bool> CheckFileExists(string fileName)
  15. {
  16. try
  17. {
  18. var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
  19. return true;
  20. }
  21. catch
  22. {
  23. }
  24. return false;
  25. }
Next I divided my project into a MVVM pattern for simplicity. So in the Model folder I placed a table class named "Contacts.cs". In ViewModels, I placed DB helpers classes (DatabaseHelperClass.cs and ReadAllContactsList.cs). And finally in the Views folder I placed all three of my UI related pages. 4. How to explore my SQLite database data

The Isolated Storage Explorer tool gets the database file. This tool is installed under the folder path Program Files (x86)\Microsoft SDKs\Windows Phone\v8.1\Tools\IsolatedStorageExplorerTool.

To use the Isolated Storage Explorer, the following things must be true:

You cannot do the following things with the Isolated Storage Explorer:

Then execute the following command from the command prompt.

  1. First change the command prompt directory path to Program Files (x86)\Microsoft SDKs\Windows Phone\v8.1\Tools\IsolatedStorageExplorerTool.

    IsolatedStorageExplorerTool

  2. Get the Product Id from the project Package.appxmanifest file in the Packaging Tab under the Package name attribute (in other words 9f68177c-0add-437b-8b43-95ec429ee5b5).

  3. If your app is run in the emulator execute this command: ISETool.exe ts xd 9f68177c-0add-437b-8b43-95ec429ee5b5 c:\data\myfiles.

    emulator execute

  4. If your app is run on the device execute this command: ISETool.exe ts de 9f68177c-0add-437b-8b43-95ec429ee5b5 c:\data\myfiles.

Now the DB content can be found in your computer at c:\data\myfiles like this.

found DB content

Note: This content might change in the future.

Summary:

In this article, we saw the basics of SQLite, how to install the engine, the library SQLite-net and saw the most common operations (also known as code first), such as inserting, updating and deleting data from a table of a database.

This article is also available at my original blog.