SQLite Operations In Windows Phone

After successfully including SQLite in our project its time to play with it. Firstly, we need to make foundation so that we can do our operations. Add a class named ContactsTable.cs in your project and write the following code in it.

  1. class ContactsTable  
  2. {  
  3.     [SQLite.PrimaryKey, SQLite.AutoIncrement]  
  4.     public int id  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     public string Name  
  10.     {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Contact  
  15.     {  
  16.         get;  
  17.         set;  
  18.     }  
  19.     public ContactsTable()  
  20.     {}  
  21.     public ContactsTable(string name, string ph_number)  
  22.     {  
  23.         Name = name;  
  24.         Contact = ph_number;  
  25.     }  
  26. }  
This is  the code for our table where our table name is contactstable.

Now go to MainPage.cs and add database path “DB_PATH” and make connection with SQLite database.
  1. public sealed partial class MainPage : Page  
  2. {  
  3.    public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "CRUD.sqlite"));  
  4.    private SQLiteConnection dbConn;  
  5. }
You provided a path to your database and create object of SQLiteConnection class.

Write the following code in OnNavigatedto().
  1. protected override void OnNavigatedTo(NavigationEventArgs e)  
  2. {  
  3.    dbConn = new SQLiteConnection(DB_PATH);  
  4.    dbConn.CreateTable<ContactsTable>();  
  5. }  
The above code makes connection with database and creates a table of name ContactsTable. Now you are good to go foundation is ready.

Let us start with Insert. Here is the code to insert data in SQLite.

Insert Operation

 

  1. private void InsertBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     ContactsTable contatsobj = new ContactsTable()  
  4.     {  
  5.         Name = nameTB.Text,  
  6.             Contact = contacttb.Text.ToString()  
  7.     };  
  8.     var query = dbConn.Insert(contatsobj); // Insert query  
  9.     //Check weather the data has been inserted or not  
  10.     if (query == null)  
  11.     {  
  12.         MessageDialog message = new MessageDialog("Failed to Insert data");  
  13.         message.ShowAsync();  
  14.     }  
  15.     else  
  16.     {  
  17.         MessageDialog message = new MessageDialog("Data has been inserted successfully");  
  18.         message.ShowAsync();  
  19.     }  
  20. }  
The data has been added successfully. Now its time to read the inserted data from the database. For this purpose we need this piece of code.

Read Operation
  1. private void read_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     var read = dbConn.Query < ContactsTable > ("select * from contactstable where name='" + nameTB.Text + "'").FirstOrDefault();  
  4.     if (read == null)  
  5.     {  
  6.         MessageDialog diag = new MessageDialog("The name you entered does not exists");  
  7.         diag.ShowAsync();  
  8.     }  
  9.     else  
  10.     {  
  11.         MessageDialog diag = new MessageDialog("ID: " + read.id + "\n" + " Name: " + read.Name);  
  12.         diag.ShowAsync();  
  13.     }  
  14. }  
Code above will read data and show you the result in Message Dialog, if result does not match it gives a message of wrong and if name matches it will show you the ID and Name in MessageDialog.

Update

Here is code for update.
  1. private void updatebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     string name = nameTB.Text;  
  4.     string setName = contacttb.Text;  
  5.     var query = dbConn.Query < ContactsTable > ("update contactstable set Name='" + setName + "' where Name='" + name + "'");  
  6.     if (query != null)  
  7.     {  
  8.         MessageDialog message = new MessageDialog("Contact has been updated successfully");  
  9.         message.ShowAsync();  
  10.     }  
  11.     else  
  12.     {  
  13.         MessageDialog message = new MessageDialog("Contact failed to update");  
  14.         message.ShowAsync();  
  15.     }  
  16. }  
The code above is very simple and easy to understand. If you are aware of SQL Update query you will easily understand this.

Delete

The following code is little bit different as I used Delete() method of SQLiteConnection class. I just got the name to delete from the database, checked it using query and then passed that query in Delete() method. Here is the code.
  1. private void deletebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     string name_to_del = nameTB.Text;  
  4.     try  
  5.     {  
  6.         var query = dbConn.Query < ContactsTable > ("select* from contactstable where Name='" + name_to_del + "'").FirstOrDefault();  
  7.         if (query != null)  
  8.         {  
  9.             dbConn.Delete(query);  
  10.             MessageDialog message = new MessageDialog("Deleted successfully");  
  11.             message.ShowAsync();  
  12.         }  
  13.     }  
  14.     catch (Exception ee)  
  15.     {  
  16.         MessageDialog message = new MessageDialog("" + ee);  
  17.         message.ShowAsync();  
  18.     }  
  19. }  
Here comes the end I tried my best to make CRUD operations simple so that programmers of all levels especially beginners can learn quickly and easily. Your suggestions will be helpful for me to write in future and for the readers too. Happy coding!

Up Next
    Ebook Download
    View all
    Learn
    View all