Setting Up SQLite In Xamarin.Forms

Introduction

In this article we will learn how to set up SQLite in Xamarin.Forms. It doesn’t require too much time and it is not difficult like setting up other databases.

Target Audience

People with basic knowledge of C# and XAML are the targeted audience of this article.

Tools

You can use both Xamarin Studio and Visual Studio for Xamarin development.

Definition

SQLite is light-weight, zero configuration and transaction database which is already available in all three mobile app platforms we are targeting. This includes Android, iOS, and Windows.

Setup

Three steps are needed to completely make things ready for development.

  • Add sqlite-net-pcl library
  • Declare interface in PCL
  • Implement interface in all projects

Add sqlite-net-pcl Library

You can find this library in NuGet Packages. Just add it in your solution by following the following simple steps.

Firstly, create a Xamarin.Forms PCL application.

Right click on Solution -> Manage NuGet Package for Solution.

Manage NuGet Package for Solution

Select "Browse" and search for sqlite-net-pcl and install it in all projects.

install

Now, move towards the second step, i.e., to make an interface in PCL.

Declare interface in PCL

To declare an interface in PCL, first make a folder named persistence, then add a class in it, so you will not be confused with other classes.

To add a class, you have to right click on solution then click on Add -> Add New Item.


Add new item

Here, you can see that I made a folder of name persistence and added a new item in it.

After this, select interface and rename it.

Interface

Add the following code in interface. 

  1. using SQLite;  
  2.   
  3. namespace XamarinApp1.Persistence  
  4. {  
  5.     public interface ISQLiteDb  
  6.     {  
  7.         SQLiteAsyncConnection GetConnection();  
  8.     }  
  9. }   

Now, implement this interface in other projects.

Implement interface in all projects

Make a folder of persistence in all projects and implement this interface just like this. Make a new class of SQLiteDb in each project and implement this interface on it.

For Android 

  1. using System;  
  2. using System.IO;  
  3. using SQLite;  
  4. using XamarinApp1.Persistence;  
  5. using XamarinApp1.Droid.Persistence;  
  6. using Xamarin.Forms;  
  7.   
  8. [assembly: Dependency(typeof(SQLiteDb))]  
  9.   
  10. namespace XamarinApp1.Droid.Persistence  
  11. {  
  12.     public class SQLiteDb : ISQLiteDb  
  13.     {  
  14.         public SQLiteAsyncConnection GetConnection()  
  15.         {  
  16.             var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  
  17.             var path = Path.Combine(documentsPath, "MySQLite.db3");  
  18.   
  19.             return new SQLiteAsyncConnection(path);  
  20.         }  
  21.     }  
  22. }   

Change namespace according to your project name. In my case, the project name is XamarinApp1

For iOS

For Android and iOS, the code is same.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using SQLite;  
  5. using XamarinApp1.Persistence;  
  6. using System.IO;  
  7. using Xamarin.Forms;  
  8. using XamarinApp1.iOS.Persistence;  
  9.   
  10. [assembly: Dependency(typeof(SQLiteDb))]  
  11.   
  12. namespace XamarinApp1.iOS.Persistence  
  13. {  
  14.     public class SQLiteDb : ISQLiteDb  
  15.     {  
  16.         public SQLiteAsyncConnection GetConnection()  
  17.         {  
  18.             var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  
  19.             var path = Path.Combine(documentsPath, "MySQLite.db3");  
  20.   
  21.             return new SQLiteAsyncConnection(path);  
  22.         }  
  23.     }  
  24. }  

For Windows

For Windows, there is a change in path of db.

  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 XamarinApp1.Persistence;  
  8. using Windows.Storage;  
  9. using System.IO;  
  10. using Xamarin.Forms;  
  11.   
  12. [assembly: Dependency(typeof(XamarinApp1.UWP.Persistence.SQLiteDb))]  
  13.   
  14. namespace XamarinApp1.UWP.Persistence  
  15. {  
  16.     public class SQLiteDb : ISQLiteDb  
  17.     {  
  18.         public SQLiteAsyncConnection GetConnection()  
  19.         {  
  20.             string documentPath = ApplicationData.Current.LocalFolder.Path;  
  21.             string path = Path.Combine(documentPath, "MySQLite.db3");  
  22.   
  23.             return new SQLiteAsyncConnection(path);  
  24.         }  
  25.     }  
  26. }  

Now, the interface is implemented and it will return the path of DB of each project.

And you are ready to use SQLite. In my next article, I will show you CRUD operations and data persistence using SQLite.