Step 1: Before going through this article, please go through my previous articles:

Step 2: First, create a Xamarin.Forms project with the Portable Class Library (PCL) as discussed in my previous article,

create
Step 3: Now, we will install sqlite-net-pcl from Nuget Package Manager.

Under PCL project, right click on References and from the Nuget Package Manager, search sqlite-net-pcl and install that within Portable Class Library (PCL) project.

sqlite-net-pcl

Step 4: Install the same SQLite library for each platform (Android, iOS and Windows) under each project. So, let’s add sqlite-net-pcl for Android first.

Right click on References under Android project and install sqlite-net-pcl in a similar way as above.

Step 5: Add an XAML Page with the name FormsPage.xaml. For this, right click on PCL project and add Forms Xaml Page.

Page

Step 6: In the FormsPage.xaml, we add two labels, two input fields and two buttons like this:
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="SQLiteTutorial.FormsPage">
  5. <ContentPage.Content>
  6. <StackLayout Padding="20">
  7. <Label Text="Name" FontSize="25"/>
  8. <Entry x:Name="memberName" Placeholder="Enter Name"></Entry>
  9. <Label Text="Age" FontSize="25"/>
  10. <Entry x:Name="memberAge" Placeholder="Enter Age"></Entry>
  11. <StackLayout Orientation="Horizontal">
  12. <Button Text="Insert Members" BackgroundColor="Gray" TextColor="White" Clicked="InsertMember"/>
  13. <Button Text="Show Memebrs" BackgroundColor="Maroon" TextColor="White" Clicked="ShowMembers"/>
  14. </StackLayout>
  15. </StackLayout>
  16. </ContentPage.Content>
  17. </ContentPage>
It will create a UI like this,

UI

Step 7: We need to create an interface class to define platform specific database file, save, and create a database connection.

So, in your PCL project, add an Interface class with the name ISQLite.

Interface

Complete code for ISQLite.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SQLiteTutorial
  7. {
  8. public interface ISQLite
  9. {
  10. SQLite.SQLiteConnection GetConnection();
  11. }
  12. }
Step 8: Next, we create a Model class with name “Member”.

Right Click on PCL Project > Add > Class > Name it Member and Click Add.

Here, Name and Age are used to set and get the values with the ID which has PrimaryKey and AutoIncrement properties derived from SQLite.

Complete Code snippet for Member class is,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SQLite;
  7. namespace SQLiteTutorial
  8. {
  9. public class Member
  10. {
  11. [PrimaryKey, AutoIncrement]
  12. public int ID { get; set; }
  13. public string Name { get; set; }
  14. public string Age { get; set; }
  15. public Member()
  16. {
  17. }
  18. }
  19. }
Step 9: Now, we add another class that represents database which includes all the logic for database operations, like Create, Read, Write, Delete, Update, etc.

Let’s add another class with name MemberDatabase for DB logics.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SQLite;
  7. using Xamarin.Forms;
  8. using System.Collections;
  9. namespace SQLiteTutorial
  10. {
  11. public class MemberDatabase
  12. {
  13. private SQLiteConnection conn;
  14. //CREATE
  15. public MemberDatabase()
  16. {
  17. conn = DependencyService.Get<ISQLite>().GetConnection();
  18. conn.CreateTable<Member>();
  19. }
  20. //READ
  21. public IEnumerable<Member> GetMembers()
  22. {
  23. var members = (from mem in conn.Table<Member>() select mem);
  24. return members.ToList();
  25. }
  26. //INSERT
  27. public string AddMember(Member member)
  28. {
  29. conn.Insert(member);
  30. return "success";
  31. }
  32. //DELETE
  33. public string DeleteMember(int id)
  34. {
  35. conn.Delete<Member>(id);
  36. return "success";
  37. }
  38. }
  39. }
Step 10: Now, we need to add platform specific database file creation code to save the database file and to create a database connection, since each platform ha a different folder environment.

FOR ANDROID

Add a class with name Andorid_SQLite and implement ISQLite interface. Under Android Project > Right Click > Add > New Class.

ANDROID

In Andorid_SQLite class, update your code with the following code snippet,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.OS;
  8. using Android.Runtime;
  9. using Android.Views;
  10. using Android.Widget;
  11. using SQLiteTutorial.Droid;
  12. using Xamarin.Forms;
  13. [assembly: Dependency(typeof(Android_SQLite))]
  14. namespace SQLiteTutorial.Droid
  15. {
  16. public class Android_SQLite : ISQLite
  17. {
  18. public SQLite.SQLiteConnection GetConnection()
  19. {
  20. var dbName = "Members.sqlite";
  21. var dbPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
  22. var path = System.IO.Path.Combine(dbPath, dbName);
  23. var conn = new SQLite.SQLiteConnection(path);
  24. return conn;
  25. }
  26. }
  27. }
FOR WINDOWS

Add a similar class like above inside Windows Phone/Windows Project. Name it Windows_SQLite.

In Windows_SQLite class, update your code with the following code snippet,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Windows.Storage;
  7. using SQLite;
  8. using System.IO;
  9. using Xamarin.Forms;
  10. using SQLiteTutorial.WinPhone;
  11. [assembly: Dependency(typeof(Windows_SQLite))]
  12. namespace SQLiteTutorial.WinPhone
  13. {
  14. public class Windows_SQLite : ISQLite
  15. {
  16. public SQLiteConnection GetConnection()
  17. {
  18. var sqliteFilename = "Member.sqlite";
  19. string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, sqliteFilename);
  20. var conn = new SQLite.SQLiteConnection(path);
  21. return conn;
  22. }
  23. }
  24. }
FOR iOS

Add a similar class like above inside iOS Project. Name it IOS_SQLite.

In IOS_SQLite class, update your code with the following code snippet,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Xamarin.Forms;
  6. using SQLite;
  7. using SQLiteTutorial.iOS;
  8. [assembly: Dependency(typeof(IOS_SQLite))]
  9. namespace SQLiteTutorial.iOS
  10. {
  11. public class IOS_SQLite : ISQLite
  12. {
  13. public SQLiteConnection GetConnection()
  14. {
  15. var dbName = "Member.sqlite";
  16. string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder
  17. string libraryPath = Path.Combine(dbPath, "..", "Library"); // Library folder
  18. var path = Path.Combine(libraryPath, dbName);
  19. var conn = new SQLite.SQLiteConnection(path);
  20. return conn;
  21. }
  22. }
  23. }
Step 11: In the code behind of FormsPage.xaml.cs, we will initialize MemberDatabase and Member class and call the AddMember function.

Click events. InsertMember and ShowMembers are defined here. And we navigate to MemberList page inside the ShowMember function.

Complete code snippet for FormsPage.xaml.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace SQLiteTutorial
  8. {
  9. public partial class FormsPage : ContentPage
  10. {
  11. public MemberDatabase memberDatabase;
  12. public Member member;
  13. public FormsPage()
  14. {
  15. InitializeComponent();
  16. }
  17. public void InsertMember(object o, EventArgs e)
  18. {
  19. member = new Member();
  20. memberDatabase = new MemberDatabase();
  21. member.Name = memberName.Text;
  22. member.Age = memberAge.Text;
  23. memberDatabase.AddMember(member);
  24. }
  25. public async void ShowMembers(object o, EventArgs e)
  26. {
  27. await Navigation.PushModalAsync(new MemberList());
  28. }
  29. }
  30. }
Step 12: In the App.cs, update App constructor with,
  1. public App()
  2. {
  3. // The root page of your application
  4. MainPage = new FormsPage();
  5. }
Step 13: Let’s add another xaml page that shows the list of members from database.

Inside PCL, Add a Forms Xaml Page with name MembersList.xaml,

MembersList

In the MemberList.xaml, add a ListView inside ContentPage.Content. Inside the ListView, we have ItemTemplate and DataTemplate. We use ViewCell to display our content.

Complete XAML code will be
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="SQLiteTutorial.MemberList">
  5. <ContentPage.Content>
  6. <ListView x:Name="listMembers" ItemTapped="OnSelected">
  7. <ListView.ItemTemplate>
  8. <DataTemplate>
  9. <ViewCell>
  10. <StackLayout Spacing="2" HorizontalOptions="Center">
  11. <StackLayout Orientation="Horizontal">
  12. <Label Text="Name: " FontSize="16"/>
  13. <Label x:Name="firstName"
  14. Text="{Binding Name}"
  15. FontSize="16"
  16. TextColor="Red"/>
  17. </StackLayout>
  18. <StackLayout Orientation="Horizontal">
  19. <Label Text="Age: " FontSize="16"/>
  20. <Label x:Name="middleName"
  21. Text="{Binding Age}"
  22. FontSize="16"
  23. TextColor="Red"/>
  24. </StackLayout>
  25. </StackLayout>
  26. </ViewCell>
  27. </DataTemplate>
  28. </ListView.ItemTemplate>
  29. </ListView>
  30. </ContentPage.Content>
  31. </ContentPage>
Step 14: In the code behind MemberList.xaml.cs, update your code with this,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace SQLiteTutorial
  8. {
  9. public partial class MemberList : ContentPage
  10. {
  11. public MemberDatabase memberDatabase;
  12. public MemberList()
  13. {
  14. InitializeComponent();
  15. memberDatabase = new MemberDatabase();
  16. var members = memberDatabase.GetMembers();
  17. listMembers.ItemsSource = members;
  18. // BindingContext = this;
  19. }
  20. public async void OnSelected(object obj, ItemTappedEventArgs args)
  21. {
  22. var member = args.Item as Member;
  23. await DisplayAlert("You selected", member.Name + " " + member.Age, "OK");
  24. }
  25. }
  26. }
Step 15: Run the application in your Android or Windows devices/emulators. When you insert members and then view them, you will be navigated to the Member List Page and see the following output.

Android Screenshots

Screenshots

Screenshots

Windows Screenshots

Windows Screenshots

Windows Screenshots

Note: Since the whole project has very large size, download the Portable Class Library (PCL) only from GitHub.