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.
- class ContactsTable
- {
- [SQLite.PrimaryKey, SQLite.AutoIncrement]
- public int id
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public string Contact
- {
- get;
- set;
- }
- public ContactsTable()
- {}
- public ContactsTable(string name, string ph_number)
- {
- Name = name;
- Contact = ph_number;
- }
- }
Now go to MainPage.cs and add database path “DB_PATH” and make connection with SQLite database.
- public sealed partial class MainPage : Page
- {
- public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "CRUD.sqlite"));
- private SQLiteConnection dbConn;
- }
Write the following code in OnNavigatedto().
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- dbConn = new SQLiteConnection(DB_PATH);
- dbConn.CreateTable<ContactsTable>();
- }
Let us start with Insert. Here is the code to insert data in SQLite.
Insert Operation
- private void InsertBtn_Click(object sender, RoutedEventArgs e)
- {
- ContactsTable contatsobj = new ContactsTable()
- {
- Name = nameTB.Text,
- Contact = contacttb.Text.ToString()
- };
- var query = dbConn.Insert(contatsobj); // Insert query
- //Check weather the data has been inserted or not
- if (query == null)
- {
- MessageDialog message = new MessageDialog("Failed to Insert data");
- message.ShowAsync();
- }
- else
- {
- MessageDialog message = new MessageDialog("Data has been inserted successfully");
- message.ShowAsync();
- }
- }
Read Operation
- private void read_Click(object sender, RoutedEventArgs e)
- {
- var read = dbConn.Query < ContactsTable > ("select * from contactstable where name='" + nameTB.Text + "'").FirstOrDefault();
- if (read == null)
- {
- MessageDialog diag = new MessageDialog("The name you entered does not exists");
- diag.ShowAsync();
- }
- else
- {
- MessageDialog diag = new MessageDialog("ID: " + read.id + "\n" + " Name: " + read.Name);
- diag.ShowAsync();
- }
- }
Update
Here is code for update.
- private void updatebtn_Click(object sender, RoutedEventArgs e)
- {
- string name = nameTB.Text;
- string setName = contacttb.Text;
- var query = dbConn.Query < ContactsTable > ("update contactstable set Name='" + setName + "' where Name='" + name + "'");
- if (query != null)
- {
- MessageDialog message = new MessageDialog("Contact has been updated successfully");
- message.ShowAsync();
- }
- else
- {
- MessageDialog message = new MessageDialog("Contact failed to update");
- message.ShowAsync();
- }
- }
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.
- private void deletebtn_Click(object sender, RoutedEventArgs e)
- {
- string name_to_del = nameTB.Text;
- try
- {
- var query = dbConn.Query < ContactsTable > ("select* from contactstable where Name='" + name_to_del + "'").FirstOrDefault();
- if (query != null)
- {
- dbConn.Delete(query);
- MessageDialog message = new MessageDialog("Deleted successfully");
- message.ShowAsync();
- }
- }
- catch (Exception ee)
- {
- MessageDialog message = new MessageDialog("" + ee);
- message.ShowAsync();
- }
- }

Asfend YarPosted Feb 26, 2016, 3:09 PM
good
Anish AnsariPosted Nov 16, 2015, 12:58 AM
nice one
Santhakumar MunuswamyPosted Nov 11, 2015, 10:20 AM
Good one
Ali AhmedPosted Nov 5, 2015, 3:09 AM
very helpful...
Yashwant VishwakarmaPosted Nov 5, 2015, 1:46 AM
Nice information..
Humayun Kabir MamunPosted Nov 5, 2015, 1:36 AM
Nice...
Sibeesh VenuPosted Nov 4, 2015, 11:20 PM
Nice Share
Harshad PansuriyaPosted Nov 4, 2015, 11:05 PM
Nice one
Anu VPosted Nov 4, 2015, 11:00 PM
good article