SQLite Database in Windows phone app – Part 3 (SQLite Exploration)

This is the third blog post of the series in which I am going to show you how to explore sqlite database stored in windows phone app.

If you have missed my last two blog posts you can refer to them from the links given below

 

SQLite Database in Windows phone app – Part 1 (Installation and Setup)

SQLite Database in Windows phone app – Part 2 (SQLite Operations)

 

Follow the steps given below

 

Step 1:
Download the IsoStoreSpy tool and SQLite Studio.

 

You can refer to the following blog post for downloading and setting up IsoStoreSpy tool.

http://windowsapptutorials.com/windows-phone/isolated-storage/how-to-access-isolated-or-local-storage-data-of-windows-phone-app-silverlightwinrt/

 


 

For SQLite Studio you can refer to the following link

http://sourceforge.net/projects/sqlitemanstudio/

 


 

Step 2:
Now using isostorespy tool you can easily export it on your pc from local storage.

 


 

Pressing the following button as highlighted in the figure you can download the sqlite database from the windows phone local storage.

 

Step 3:
Open the downloaded database file using SQLite studio.

 


 

You can browse the contents of your database easily.

 


 

I hope this post will be helpful to you.


SQLite Database in Windows phone app – Part 2 (SQLite Operations)

In my last blog post I have explained you how to install and setup SQLite Database in your Windows Phone app Project. In this post I will explain you how to create or delete a database and how to perform different operations like insertion, deletion & searching etc.

We will be working on the same project that we have created in last post. So if you have missed that post you can view it from the link given below. And follow the steps given below.

SQLite Database in Windows phone app – Part 1 (Installation and Setup)

 

Step 1: Create a new folder called Database in your project and add a class to it having the name Schema.cs.

 


 

Step 2: Now create a table called Students in form of class that we need to include in our Records.sqlite database.
The class having the name Students with all the getter and setter methods have been given below.

 


 

 

Note: One property needs to act as primary key and its value should be unique for each record. In this example Id of student acts as primary key.

Step 3: So it’s time to perform all SQLite operations. So I thought to make a single helperclass called ‘DatabaseHelper.cs’ in Database folder and handle all the SQLite operations with this helperclass.

 

 

Step 4: This is the main step where we will be writing all the SQLite operations in the helperclass.

 

 

1. Creating a Database While creating a database we need to first check whether it exists or not. If not then create a new database. The code for the following is given below.

 

 

Note: You can create more than one table in a database. In this post we have just created one table to keep things simple.

2. Deleting a Database
Note that database is created as a storage file in Windows Phone app so to delete it you only need to delete the following storage file. The code for the following is given below.

 

 
 3. Adding a new record The best practice while adding a new record in the database is to first check whether database with following name exists previously or not and then insert a new record. The code for the following is given below.  

 


 
4. Updating an existing record
To update a record in database we first need to query that object from the database and then only we can make changes to it. The code for the following is given below.

 

 

Note: In this example we are updating the marks of the student with given id using the search query.

5. Deleting an existing record Similar to updating an existing record you first need to query the record from the database and then perform the deletion operation. The code for the following is given below.

 


 
6. Selecting all students from the database
The code for fetching all the records from student table is given below.

 

 
Note: The full code for the DatabaseHelper.cs is given in this link.

Step 5: Now after writing the code for database operations the next step is to create the UI for the above and bind the data to it. On MainPage.xaml create 4 different buttons as shown in the figure below.

 

 

The code for the Design view is as following  
 

 
Different buttons are

1. Create Button– This button is used to create a new database. The code for the click event handler is as given below

 
 
 

2. Delete Button– This button is used to delete the current database if exists. The code for click event handler is given below.

 
 
 
3. Add a new record Button– This button navigates to new page. Where we can add a new record into the students table. The code for click event handler is given below.

 

 

4. Display all records Button– This button navigates to a new page. Where we can view all the records and delete them from the table. The code for click event handler is given below.  

 

 

Step 6: Create two new pages having the name AddRecord.xaml and Display.Xaml in your project.

 


 

AddRecord Page

In this page we insert a new record into the table

 


 

The code for xaml page is given below

 

 

When use clicks on the insert button the record is inserted into the database. The code for the click event handler is given below.

 

 

Display Page

In this page we display all the records that are present in the database as shown in the figure below.

 

 

The code for the xaml is

 

 

And the code for the Display.xaml.cs is as following

 

 

Note: In both of the pages we have made use of the functions defined in DatabaseHelper.cs.

 

Step 7: Add the following code for back button press in App.xaml.cs to control the navigation from one page to another.

 


 


 
If you have any doubts related to this post you can download the full source code of the project from the link given below.

SQLiteDatabase.zip

In my next blog post I shall explain you how to explore SQLite Database using IsoStoreExplorer tool.

http://windowsapptutorials.com/windows-phone-8-1/sqlite-database-in-windows-phone-app-part-3-sqlite-exploration/

SQLite Database in Windows phone app – Part 1 (Installation and Setup)

While developing applications there may be a need to store data and access it for later use. Windows Phone provides us with different ways to store our application data depending on the size. But when dealing with large data involving multiple entries, database is the appropriate method.

 

Windows phone 8.1 Runtime app supports SQLite database. So in this article I will explain you how to install and setup SQLite database reference in your windows phone application.

Follow the steps given below

 

Step 1:
Create a new Project having the name SQLiteDatabase.

 


 

Step 2:
Install the SQLite for Windows phone 8.1 SDK

Go to Tools->Extensions and Updates

 


 

Then type the keyword sqlite in search bar in the online tab. And install the SDK SQLite for Windows Phone 8.1

 


 

Note: If you don’t find the required library in Visual Studio gallery you can download it from the required web page.

 

Step 3:
Add the SQLite Reference to your Project

 


 

Go to References->Windows Phone 8.1->Extensions

 


 

When done, you will see that the proper references to SQLite and Visual C++ 2013 Runtime have been added to your project

 


 

Step 4:
Change the target Platform of your Project

You may have already noticed, that the references show a warning symbols, to resolve that the next thing to do, before compiling the solution is changing the architecture of the target platform. This is the fact that the engine of Sqlite is written in C ++, and the default target platform set in the project is Any CPU. This mode is not supported.

So in order to resolve this go to Configuration Manager as shown in the figure below

 


 

In the next dialog, we note that we have several choices of platforms, Any CPU (the default), ARM, x64 and x86.

Now we change the target platform accordingly. If you want to debug your app on Phone then choose the ARM Platform. But if you wish to debug your app on Emulator or Windows PC select x86 or x64 configuration depending on your processor if it is 32 bits or 64 bits.

 


 

Note: Every time you change your debugging option from emulator to phone you need to change the debug configuration accordingly.

 

Step 5:
Installing sqlite-net package

After installing the library for SQLite, we need sqlite-net NuGet package. With this library, we will be able to perform all the operations that you normally do in a database, such as Insert, Delete, Update and run search queries. This package provides two helper classes (SQLite.cs and SQLiteAsync.cs).

To install it right click on the project->Manage NuGet Packages and search for sqlite-net

 


 

Finally we have completed the SQLite setup. In the next blog post I will be explaining you how to perform different operations like insertion, deletion and searching in SQLite database.

 

http://windowsapptutorials.com/windows-phone/sqlite-database-in-windows-phone-app-part-2-sqlite-operations