In this recipe let's explore how to use the SQLite file we created in the first recipe, Overview of SQLite Studio, as the local data store for our MyTasks
app.
For this recipe we shall use the project template we created in the Chapter 1,Data Binding to UI Elements.
In the following steps, we will create a MyTasks
project to use the sample SQLite database file we created in the preceding recipe and then learn how to use the SQLite Client API to insert and display the results.
CH1_MyTasks
template and name it Ch5_MyTasks_SQLiteClient
. MyDatabase.sqlite
we created in the first recipe, Overview of SQLite Studio, and add it to the project root folder. DBHelper.cs
file from the SQLite Client project in the new folder Helpers
. Community.CsharpSqlite.WP.dll
. App.xaml.cs
file and add using
declaratives at the top of the file:using Ch5_MyTasks_SQLiteClient.Helpers; using System.Reflection;
App
class, declare the following get
property:private DBHelper _db; public DBHelper db { get { Assembly assem = Assembly.GetExecutingAssembly(); if (_db == null) _db = new DBHelper(assem.FullName.Substring(0, assem.FullName.IndexOf(',')), "MyDatabase.sqlite"); return _db; } }
MainPage.xaml
and add the declaration for myTasks
collections:private ObservableCollection<DataClass> myTasks;
IntializeList
, which will query the data collection class and return the results. The result from the query is databound to a list box control's ItemsSource:
private void InitalizeList() { string selectQuery = "Select * from MyTasks"; myTasks = (Application.Current as App). db.SelectObservableCollection<DataClass>(selectQuery); lstTasks.ItemsSource = myTasks; }
Main_Loaded
event, call the method InitializeList()
. .cs
file, and replace the Add_Click
method, as follows:private void btnAdd_Click(object sender, RoutedEventArgs e) { DateTime createdDate = DateTime.Now; int rec; string strInsert = "Insert into MyTasks values (@Name,@Notes,@Priority, @DateDue)"; MyTasks newTask = new MyTasks { Name = txtName.Text.ToString(), Notes = txtNotes.Text.ToString(), Priority = txtPriority.Text.ToString(), DateDue = DateTime.Parse(txtPrioirty.Text.ToString()); }; rec = (Application.Current as App).db.Insert <MyTasks>(newTask, strInsert); }
We initially loaded the database file using the DBHelper
class. Then, we used the select
query and executed it to return the results. The list is then assigned to a list box for display. Once we have the list displayed, we then add the code to insert the task using the form. Here we used an insert
SQL statement.
This recipe demonstrates how easy it is to implement the popular SQLite database as an embedded database in your apps. You can reuse SQLite across other mobile platforms as well. So this makes the SQLite a very portable solution for multiple platforms.
Check the following recipe to see how SQL CE is used as an embedded database. In the following two recipes, we discuss other options available as local database storage. Also, check Chapter 7, Windows Communication Framework - WCF, which discusses how to store the data externally.