SQLite With Xamarin Forms Step By Step Guide

In many mobile applications it’s a requirement to store data locally and for that purpose we use databases with the applications. The type of database to use mainly depends upon the requirement of the application, but in most of MIS (Management Information Systems) based application relational databases are used for this purpose.

The most commonly used relational database with Xamarin Forms is SQLite. It is the best suited relational database for mobile applications as it has very small footprint. This article will be an step by step guide on how to use a SQLite database with a Xamarin Forms application. We will be using the database to persist employee data. The structure of the database will be like following Diagram:

table

Step 1: 

Create a new project in Xamarin/Visual Studio (see this article for steps). Name it as ”. There is no out of the box tool available in Xamarin Forms, so we will be using a third party plugin for this purpose and the most popular plugin is ‘SQLite.Net-PCL’ created by oysteinkrog.

Step 2:


In order to use ‘SQLite.Net-PCL’ plugin, add the nuget package of the plugin to each project of the solution. In Visual Studio this can be done by right click on the solution and selecting ‘Manage NuGet Packages for Solution‘ option. Which will give following screen:

SQLite Blog Nuget Add

In case of Xamarin Studio, you will have to individually add the NuGet package by right clicking on the‘Packages’ folder and selecting ‘Add Packages’ option on each project.

Even tough the ‘SQLite.Net-PCL‘ package will provide us the functionality of manipulating the SQLite database, it can’t automatically initialize the database connection object as the location of the database file varies on different platforms. So in order to solve this issue we will use dependency service to load the database file in connection object.

Step 3:
Create a blank interface with one method signature like following code,

  1. using SQLite.Net;  
  2.    
  3. namespace SQLiteEx  
  4. {  
  5.     public interface ISQLite  
  6.     {  
  7.         SQLiteConnection GetConnection();  
  8.     }  
  9. }  
Step 4:

Create Class file named ‘SQLiteService‘ implementing ‘ISQLite‘ interface and Write the implementation of this ‘GetConnection‘ in every platform specific code like following given codes: 

For new database: When we are creating a database in the application itself for the first time then following code will be used.

Android:

  1. using System;  
  2. using Xamarin.Forms;  
  3. using SQLiteEx.Droid;  
  4. using System.IO;  
  5.    
  6. [assembly: Dependency(typeof(SqliteService))]  
  7. namespace SQLiteEx.Droid  
  8. {  
  9.     public class SqliteService : ISQLite  
  10.     {  
  11.         public SqliteService() { }  
  12.   
  13.         #region ISQLite implementation  
  14.         public SQLite.Net.SQLiteConnection GetConnection()  
  15.         {  
  16.             var sqliteFilename = "SQLiteEx.db3";  
  17.             string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder  
  18.             var path = Path.Combine(documentsPath, sqliteFilename);  
  19.             Console.WriteLine(path);  
  20.             if (!File.Exists(path)) File.Create(path);  
  21.             var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();  
  22.             var conn = new SQLite.Net.SQLiteConnection(plat, path);  
  23.             // Return the database connection   
  24.             return conn;  
  25.         }  
  26.   
  27.         #endregion  
  28.    
  29.    
  30.     }  
  31. }  

IOS:

  1. using SQLiteEx.iOS;  
  2. using System;  
  3. using System.IO;  
  4. using Xamarin.Forms;  
  5.    
  6. [assembly: Dependency(typeof(SqliteService))]  
  7. namespace SQLiteEx.iOS  
  8. {  
  9.     public class SqliteService : ISQLite  
  10.     {  
  11.         public SqliteService()  
  12.         {  
  13.         }  
  14.         #region ISQLite implementation  
  15.         public SQLite.Net.SQLiteConnection GetConnection()  
  16.         {  
  17.             var sqliteFilename = "SQLiteEx.db3";  
  18.             string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder  
  19.             string libraryPath = Path.Combine(documentsPath, "..""Library"); // Library folder  
  20.             var path = Path.Combine(libraryPath, sqliteFilename);  
  21.    
  22.             // This is where we copy in the prepopulated database  
  23.             Console.WriteLine(path);  
  24.             if (!File.Exists(path))  
  25.             {  
  26.                 File.Create(path);  
  27.             }  
  28.    
  29.             var plat = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();  
  30.             var conn = new SQLite.Net.SQLiteConnection(plat, path);  
  31.    
  32.             // Return the database connection   
  33.             return conn;  
  34.         }  
  35.         #endregion  
  36.     }  
  37. }  

For Pre-Created Database:

In some application scenarios there may be a requirement that the database is pre-populated with some data and we are going to use the same in the application. In such scenarios first copy the database file at following mentioned locations in platform specific project and then use following code in ‘SQLiteService’ Class.

Android:

Copy the database file in ‘Resources\Raw’ folder of Android platform project.

  1. using System;  
  2. using Xamarin.Forms;  
  3. using SQLiteEx.Droid;  
  4. using System.IO;  
  5.    
  6. [assembly: Dependency(typeof(SqliteService))]  
  7. namespace SQLiteEx.Droid  
  8. {  
  9.        
  10.     public class SqliteService : ISQLite {  
  11.    
  12.         public SqliteService () {}  
  13.   
  14.         #region ISQLite implementation    
  15.         public SQLite.Net.SQLiteConnection GetConnection ()  
  16.         {  
  17.             var sqliteFilename = "SQLiteEx.db3";  
  18.             string documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal); // Documents folder  
  19.             var path = Path.Combine(documentsPath, sqliteFilename);  
  20.    
  21.             // This is where we copy in the prepopulated database  
  22.             Console.WriteLine (path);  
  23.             if (!File.Exists(path))  
  24.             {  
  25.                 var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.APGameDb);  // RESOURCE NAME ###  
  26.    
  27.                 // create a write stream  
  28.                 FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);  
  29.                 // write to the stream  
  30.                 ReadWriteStream(s, writeStream);  
  31.             }  
  32.    
  33.             var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();  
  34.             var conn = new SQLite.Net.SQLiteConnection(plat, path);  
  35.    
  36.             // Return the database connection   
  37.             return conn;  
  38.         }  
  39.         #endregion  
  40.    
  41.         ///  
  42. <summary>  
  43.         /// helper method to get the database out of /raw/ and into the user filesystem  
  44.         /// </summary>  
  45.    
  46.         void ReadWriteStream(Stream readStream, Stream writeStream)  
  47.         {  
  48.             int Length = 256;  
  49.             Byte[] buffer = new Byte[Length];  
  50.             int bytesRead = readStream.Read(buffer, 0, Length);  
  51.             // write the required bytes  
  52.             while (bytesRead > 0)  
  53.             {  
  54.                 writeStream.Write(buffer, 0, bytesRead);  
  55.                 bytesRead = readStream.Read(buffer, 0, Length);  
  56.             }  
  57.             readStream.Close();  
  58.             writeStream.Close();  
  59.         }  
  60.    
  61.    
  62.     }  
  63. }  

IOS:

Copy the database file in ‘Resources’ folder of Android platform project.

  1. using SQLiteEx.iOS;  
  2. using System;  
  3. using System.IO;  
  4. using Xamarin.Forms;  
  5.    
  6. [assembly: Dependency(typeof(SqliteService))]  
  7. namespace SQLiteEx.iOS  
  8. {  
  9.     public class SqliteService : ISQLite  
  10.     {  
  11.         public SqliteService()  
  12.         {  
  13.         }  
  14.         #region ISQLite implementation  
  15.         public SQLite.Net.SQLiteConnection GetConnection()  
  16.         {  
  17.             var sqliteFilename = "SQLiteEx.db3";  
  18.             string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder  
  19.             string libraryPath = Path.Combine(documentsPath, "..""Library"); // Library folder  
  20.             var path = Path.Combine(libraryPath, sqliteFilename);  
  21.    
  22.             // This is where we copy in the prepopulated database  
  23.             Console.WriteLine(path);  
  24.             if (!File.Exists(path))  
  25.             {  
  26.                 File.Copy(sqliteFilename, path);  
  27.             }  
  28.    
  29.             var plat = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();  
  30.             var conn = new SQLite.Net.SQLiteConnection(plat, path);  
  31.    
  32.             // Return the database connection   
  33.             return conn;  
  34.         }  
  35.         #endregion  
  36.     }  
  37. }  

Step 5:

Create a class in PCL project named as ‘DataAccess‘. This class will contain all the data access codes for the application. The code of the class is as follows:

  1. using SQLite.Net;  
  2. using Xamarin.Forms;  
  3.    
  4. namespace SQLiteEx  
  5. {  
  6.     public class DataAccess  
  7.     {  
  8.         SQLiteConnection dbConn;  
  9.         public DataAccess()  
  10.         {  
  11.             dbConn = DependencyService.Get<ISQLite>().GetConnection();  
  12.             // create the table(s)  
  13.             dbConn.CreateTable<Employee>();  
  14.         }  
  15.         public List<Employee> GetAllEmployees()  
  16.         {  
  17.             return dbConn.Query<Employee>("Select * From [Employee]");  
  18.         }  
  19.         public int SaveEmployee(Employee aEmployee)  
  20.         {  
  21.             return dbConn.Insert(aEmployee);              
  22.         }  
  23.         public int DeleteEmployee(Employee aEmployee)  
  24.         {  
  25.             return dbConn.Delete(aEmployee);  
  26.         }  
  27.         public int EditEmployee(Employee aEmployee)  
  28.         {  
  29.             return dbConn.Update(aEmployee);  
  30.         }  
  31.     }  
  32. }  

As it can be seen from the code above that the application is using dependency service to fetch the database and create the connection object in the constructor of the class which will be used in order to manipulate the data. Secondly the code to create tables are there in constructor also, this we will have to give even if we are using a pre-created database as the method automatically checks for the table in database and create it if not present.

Step 6:


Create the Plain Old CLR Object (POCO) classes for the table(s) present in the database. For Example ‘Employee‘ class for the above given tables in ‘DataAccess‘ class.

Employee:

  1. using SQLite.Net.Attributes;  
  2. using System;  
  3.    
  4. namespace SQLiteEx  
  5. {  
  6.     public class Employee  
  7.     {  
  8.         [PrimaryKey, AutoIncrement]  
  9.         public long EmpId  
  10.         { getset; }  
  11.         [NotNull]  
  12.         public string EmpName  
  13.         { getset; }  
  14.         public string Designation  
  15.         { getset; }  
  16.         public string Department  
  17.         { getset; }  
  18.         public string Qualification  
  19.         { getset; }  
  20.     }  
  21. }  

Step 7:

Create the static object property of ‘DataAccess‘ in ‘App‘ class like following code. This will enable us to use manipulate the data from any screen of the application.

  1. using Xamarin.Forms;  
  2. using Xamarin.Forms.Xaml;  
  3.    
  4. [assembly: XamlCompilation(XamlCompilationOptions.Compile)]  
  5. namespace SQLiteEx  
  6. {  
  7.     public class App : Application  
  8.     {  
  9.         static DataAccess dbUtils;  
  10.         public App()  
  11.         {  
  12.             // The root page of your application  
  13.             MainPage = new NavigationPage(new ManageEmployee());  
  14.         }  
  15.         public static DataAccess DAUtil  
  16.         {  
  17.             get  
  18.             {  
  19.                 if (dbUtils == null)  
  20.                 {  
  21.                     dbUtils = new DataAccess();  
  22.                 }  
  23.                 return dbUtils;  
  24.             }  
  25.         }  
  26.         protected override void OnStart()  
  27.         {  
  28.             // Handle when your app starts  
  29.         }  
  30.    
  31.         protected override void OnSleep()  
  32.         {  
  33.             // Handle when your app sleeps  
  34.         }  
  35.    
  36.         protected override void OnResume()  
  37.         {  
  38.             // Handle when your app resumes  
  39.         }  
  40.     }  
  41. }  

This completes the basic structure of application using a SQLite database to persist data of the application.

Now let’s create the screens of our sample application to see the CRUD operation code.The Application will contain following screens:

  1. Manage Employees
  2. Add Employee
  3. Show Employee Details
  4. Edit Employee

Manage Employees:

This screen is the home screen of the application, it will show the current list of Employees and give an option to add new employee, view the details of the Employee and manage departments. The XAML code of this page will be like :

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SQLiteEx.ManageEmployee" Title="Manage Employees" >  
  3.   <ContentPage.Padding>  
  4.     <OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0" />  
  5.   </ContentPage.Padding>  
  6.   <ContentPage.Content>  
  7.     <ListView x:Name="lstData" HasUnevenRows="false" Header="Header Value" Footer="Footer" ItemSelected="OnSelection" >  
  8.       <ListView.HeaderTemplate>  
  9.         <DataTemplate>  
  10.           <StackLayout Orientation="Horizontal" BackgroundColor="Blue" Padding="5,5,5,5">  
  11.             <Label Text="Name" FontSize="Medium" FontAttributes="Bold" TextColor="White"/>  
  12.             <Label Text="Designation" FontSize="Medium" FontAttributes="Bold" TextColor="White"/>  
  13.             <Label Text="Department" FontSize="Medium" FontAttributes="Bold" TextColor="White"/>  
  14.           </StackLayout>         
  15.         </DataTemplate>  
  16.       </ListView.HeaderTemplate>  
  17.       <ListView.ItemTemplate>  
  18.         <DataTemplate>  
  19.           <ViewCell>  
  20.             <StackLayout Orientation="Horizontal" Padding="5,5,5,5">  
  21.               <Label Text="{Binding EmpName}" FontSize="Medium" />  
  22.               <Label Text="{Binding Designation}" FontSize="Medium" />  
  23.               <Label Text="{Binding Department}" FontSize="Medium" />  
  24.             </StackLayout>  
  25.           </ViewCell>  
  26.         </DataTemplate>  
  27.       </ListView.ItemTemplate>  
  28.       <ListView.FooterTemplate>  
  29.         <DataTemplate>  
  30.           <StackLayout Orientation="Horizontal" Padding="5,5,5,5">  
  31.             <Button Text="Add New Employee" Clicked="OnNewClicked" />  
  32.           </StackLayout>  
  33.         </DataTemplate>  
  34.       </ListView.FooterTemplate>  
  35.     </ListView>  
  36.   </ContentPage.Content>  
  37. </ContentPage>  

As per the above XAML, the application executes ‘OnSelection‘ method on ‘ItemSelected‘ event of list to show the detailed employee information, ‘OnNewClicked‘ method on ‘Clicked‘ event of ‘Add New Employee‘ button. The code behind of this page will contain following code.

  1. using System;  
  2. using Xamarin.Forms;  
  3.    
  4. namespace SQLiteEx  
  5. {  
  6.     public partial class ManageEmployee : ContentPage  
  7.     {  
  8.         public ManageEmployee()  
  9.         {  
  10.             InitializeComponent();  
  11.             var vList = App.DAUtil.GetAllEmployees();  
  12.             lstData.ItemsSource = vList;  
  13.         }  
  14.    
  15.         void OnSelection(object sender, SelectedItemChangedEventArgs e)  
  16.         {  
  17.             if (e.SelectedItem == null)  
  18.             {  
  19.                 return;   
  20.                 //ItemSelected is called on deselection,   
  21.                 //which results in SelectedItem being set to null  
  22.             }  
  23.             var vSelUser = (Employee)e.SelectedItem;  
  24.             Navigation.PushAsync(new ShowEmplyee(vSelUser));  
  25.         }  
  26.         public void OnNewClicked(object sender, EventArgs args)  
  27.         {  
  28.             Navigation.PushAsync(new AddEmployee());  
  29.         }  
  30.     }  
  31. }  

Add Employee:

As the name suggests this page will be used for adding a new employee and will be called upon click of ‘Add New Employee’ button on ‘Manage Employee’ page. The XAML code of this page will be like:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SQLiteEx.AddEmployee" Title="Add New Employee">  
  3.       <ContentView.Content>  
  4.               <TableView Intent="Settings" BackgroundColor="White">  
  5.                   <TableRoot>  
  6.                       <TableSection Title="Add New Employee">  
  7.                           <EntryCell x:Name="txtEmpName" Label="Employee Name" Keyboard="Text" />  
  8.                           <EntryCell x:Name="txtDesign" Label="Designation" Keyboard="Text" />  
  9.                           <EntryCell x:Name="txtDepartment" Label="Department" Keyboard="Text" />  
  10.                           <EntryCell x:Name="txtQualification" Label="Qualification" Keyboard="Text" />               
  11.                           <ViewCell>  
  12.                               <ContentView Padding="0,0">  
  13.                   <ContentView.Padding>  
  14.                     <OnPlatform x:TypeArguments="Thickness" iOS="10,0" WinPhone="0,15,0,0" />  
  15.                   </ContentView.Padding>  
  16.                   <ContentView.Content>  
  17.                     <Button BackgroundColor="#fd6d6d" Text="Save" TextColor="White" Clicked="OnSaveClicked" />  
  18.                   </ContentView.Content>  
  19.                 </ContentView>  
  20.                           </ViewCell>  
  21.                       </TableSection>  
  22.                   </TableRoot>  
  23.               </TableView>  
  24.       </ContentView.Content>  
  25. </ContentPage>  

The code behind of this page will contain following code:

  1. using System;  
  2. using Xamarin.Forms;  
  3.    
  4. namespace SQLiteEx  
  5. {  
  6.     public partial class AddEmployee : ContentPage  
  7.     {  
  8.         public AddEmployee()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.    
  13.         public void OnSaveClicked(object sender, EventArgs args)  
  14.         {  
  15.             var vEmployee = new Employee()  
  16.             {  
  17.                 EmpName = txtEmpName.Text,  
  18.                 Department = txtDepartment.Text,  
  19.                 Designation = txtDesign.Text,  
  20.                 Qualification = txtQualification.Text  
  21.             };  
  22.             App.DAUtil.SaveEmployee(vEmployee);  
  23.             Navigation.PushAsync(new ManageEmployee());  
  24.         }  
  25.     }  
  26. }  

Show Employee Details:

As the name suggests, this page will be used to show the complete details of the employee and also will have the option to invoke ‘Edit Employee’ page and ‘Delete Employee’ to delete the details of employee from database. The XAML code of this page is like,

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SQLiteEx.ShowEmplyee" Title="View Employee">  
  3.   <ContentView.Content>  
  4.     <Grid>  
  5.       <Grid.RowDefinitions>  
  6.         <RowDefinition Height="*"/>  
  7.         <RowDefinition Height="*"/>  
  8.         <RowDefinition Height="*"/>  
  9.         <RowDefinition Height="*"/>  
  10.         <RowDefinition Height="*"/>  
  11.         <RowDefinition Height="*"/>  
  12.         <RowDefinition Height="*"/>  
  13.       </Grid.RowDefinitions>  
  14.    
  15.       <Grid.ColumnDefinitions>  
  16.         <ColumnDefinition Width="10"/>  
  17.         <ColumnDefinition Width="*"/>  
  18.         <ColumnDefinition Width="*"/>  
  19.         <ColumnDefinition Width="10"/>  
  20.       </Grid.ColumnDefinitions>  
  21.    
  22.       <Label Grid.Row ="0" Grid.Column="0" Grid.ColumnSpan="2" Text="Employee Details" />  
  23.       <Label Grid.Row ="1" Grid.Column="1" Text="Name" />  
  24.       <Label Grid.Row="1" Grid.Column="2" Text ="{Binding EmpName}" />  
  25.       <Label Grid.Row ="2" Grid.Column="1" Text="Designation" />  
  26.       <Label Grid.Row="2" Grid.Column="2" Text ="{Binding Designation}"/>  
  27.       <Label Grid.Row ="3" Grid.Column="1" Text="Department" />  
  28.       <Label Grid.Row="3" Grid.Column="2" Text ="{Binding Department}"/>  
  29.       <Label Grid.Row ="4" Grid.Column="1" Text="Qualification" />  
  30.       <Label Grid.Row="4" Grid.Column="2" Text ="{Binding Qualification}" />  
  31.       <Button Grid.Row ="5" Grid.Column="1" Text="Edit Details" Clicked="OnEditClicked" />  
  32.       <Button Grid.Row="5" Grid.Column="2" Text="Delete" Clicked="OnDeleteClicked" />  
  33.     </Grid>  
  34.   </ContentView.Content>  
  35. </ContentPage>  

And the code behind of this page will contain following code:

  1. using System;  
  2. using Xamarin.Forms;  
  3.    
  4. namespace SQLiteEx  
  5. {  
  6.     public partial class ShowEmplyee : ContentPage  
  7.     {  
  8.         Employee mSelEmployee;  
  9.         public ShowEmplyee(Employee aSelectedEmp)  
  10.         {  
  11.             InitializeComponent();  
  12.             mSelEmployee = aSelectedEmp;  
  13.             BindingContext = mSelEmployee;  
  14.         }  
  15.    
  16.         public void OnEditClicked(object sender, EventArgs args)  
  17.         {  
  18.             Navigation.PushAsync(new EditEmployee(mSelEmployee));  
  19.         }  
  20.         public async void  OnDeleteClicked(object sender, EventArgs args)  
  21.         {  
  22.             bool accepted = await DisplayAlert("Confirm""Are you Sure ?""Yes""No");  
  23.             if (accepted)  
  24.             {  
  25.                 App.DAUtil.DeleteEmployee(mSelEmployee);  
  26.             }  
  27.             await Navigation.PushAsync(new ManageEmployee());  
  28.         }  
  29.     }  
  30. }  

Edit Employee: As the name suggests, this page will be used to edit the details of the employee. The XAML code of this page is like,

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SQLiteEx.EditEmployee" Title="Edit Employee">  
  3.   <ContentView.Content>  
  4.     <TableView Intent="Settings" BackgroundColor="White">  
  5.       <TableRoot>  
  6.         <TableSection Title="Edit Employee">  
  7.           <EntryCell x:Name="txtEmpName" Label="Employee Name" Text ="{Binding EmpName}" Keyboard="Text" />  
  8.           <EntryCell x:Name="txtDesign" Label="Designation" Text ="{Binding Designation}" Keyboard="Text" />  
  9.           <EntryCell x:Name="txtDepartment" Label="Department" Text ="{Binding Department}" Keyboard="Text" />  
  10.           <EntryCell x:Name="txtQualification" Label="Qualification" Text ="{Binding Qualification}" Keyboard="Text" />  
  11.           <ViewCell>  
  12.             <ContentView Padding="0,0">  
  13.               <ContentView.Padding>  
  14.                 <OnPlatform x:TypeArguments="Thickness" iOS="10,0" WinPhone="0,15,0,0" />  
  15.               </ContentView.Padding>  
  16.               <ContentView.Content>  
  17.                 <Button BackgroundColor="#fd6d6d" Text="Save" TextColor="White" Clicked="OnSaveClicked" />  
  18.               </ContentView.Content>  
  19.             </ContentView>  
  20.           </ViewCell>  
  21.         </TableSection>  
  22.       </TableRoot>  
  23.     </TableView>  
  24.   </ContentView.Content>  
  25. </ContentPage>  

And the code behind of this page will contain following code:

  1. using System;  
  2. using Xamarin.Forms;  
  3.    
  4. namespace SQLiteEx  
  5. {  
  6.     public partial class EditEmployee : ContentPage  
  7.     {  
  8.         Employee mSelEmployee;  
  9.         public EditEmployee(Employee aSelectedEmp)  
  10.         {  
  11.             InitializeComponent();  
  12.             mSelEmployee = aSelectedEmp;  
  13.             BindingContext = mSelEmployee;  
  14.         }  
  15.    
  16.         public void OnSaveClicked(object sender, EventArgs args)  
  17.         {  
  18.             mSelEmployee.EmpName = txtEmpName.Text;  
  19.             mSelEmployee.Department = txtDepartment.Text;  
  20.             mSelEmployee.Designation = txtDesign.Text;  
  21.             mSelEmployee.Qualification = txtQualification.Text;  
  22.             App.DAUtil.EditEmployee(mSelEmployee);  
  23.             Navigation.PushAsync(new ManageEmployee());  
  24.         }  
  25.     }  
  26. }  

This is how the application looks on iOS Simulator:

SQLiteExBlogiOS-Ex

As mentioned earlier in the article, this article contains a step by step guide to use SQLite database in Xamarin Forms mobile application. The example code of this article can be found at GitHub. Let me know if I have missed anything or if you have any suggestions.

Reference: Xamarin Forms Documentation

 
Read more articles on Xamarin:
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all