Setting Up SQLite Connection In Xamarin App

Introduction

In this article, we are going to learn how to set up a connection with SQLite in our application. We will connect each of the iOS, Android, and UWP projects with SQLite.

Targeted Audience

People with the basic knowledge of C# and Xamarin.

Tools

Visual Studio with Xamarin installed.

SQLite is the most preferred embedded database for mobile developers. The reason behind is that it provides a very simple, serverless, zero-configuration setup and also, it is free and open source. Setting up a connection with SQLite is easy. Let’s see how to do this in the following simple steps.

  1. Install NuGet package to your solution “SQLite-net-pcl”.
  2. Create a model matching the SQLite Database table definition in the Xamarin common base project.
  3. Create an interface in it.
  4. Implement the interface in each iOS, Android, and UWP project.
  5. Register each of them with the SQL Dependency Services.

First of all, you have to go to your solution and right-click on it to manage the NuGet packages. Type sqlite-net-pcl to add the package for the database as done below in the picture.

In this article we are going to learn about how to set up connection with SQLite in your application. We will connect each of the iOS, Android and UWP projects with the SQLite. 

Click the "Install" button to install the package and don’t forget to choose the latest stable version.

In this article we are going to learn about how to set up connection with SQLite in your application. We will connect each of the iOS, Android and UWP projects with the SQLite. 

Next, you have to add a folder in your base project with a class. Here, I am adding a “Connections” folder with the class name “ISqlConnection.cs”. The naming convention is not necessary; you can use your own as you like.

In this article we are going to learn about how to set up connection with SQLite in your application. We will connect each of the iOS, Android and UWP projects with the SQLite. 

Create an interface in this class with a public access modifier and type the following syntax in it 

SQLiteAsyncConnection Connection();
 
It will come up with an error but don’t get worried, this is just because you did not add the namespace. So, add the namespace on the top “using SQLite” and it will fix the issues.
 
In this article we are going to learn about how to set up connection with SQLite in your application. We will connect each of the iOS, Android and UWP projects with the SQLite.
 
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using SQLite;  
  5.   
  6. namespace PracApp1.Connections  
  7. {  
  8.     public interface ISqlConnection  
  9.     {  
  10.         SQLiteAsyncConnection Connection();  
  11.     }  
  12. }  

Now, you just have to implement the interface in all three native projects. And register with the dependency services. So, add a class in your Android project like in the picture below.

In this article we are going to learn about how to set up connection with SQLite in your application. We will connect each of the iOS, Android and UWP projects with the SQLite. 

After that, you have to implement the interface in it. And register it with the SQL dependency. As you can see in the code below, the SqlConnection class is implemented with the interface ISqlConnection. And the function “Connection” returns the SQLiteAsyncConnection with the path.

C# Code for Android
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6.   
  7. using Android.App;  
  8. using Android.Content;  
  9. using Android.OS;  
  10. using Android.Runtime;  
  11. using Android.Views;  
  12. using Android.Widget;  
  13. using PracApp1.Connections;  
  14. using PracApp1.Droid;  
  15. using SQLite;  
  16. using Xamarin.Forms;  
  17.   
  18. [assembly: Dependency(typeof(SqlConnection))]  
  19.   
  20. namespace PracApp1.Droid  
  21. {  
  22.     class SqlConnection : ISqlConnection  
  23.     {  
  24.         public SQLiteAsyncConnection Connection()  
  25.         {  
  26.             var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);  
  27.             var path = Path.Combine(documentsPath, "MySQLite.sqldb");  
  28.   
  29.             return new SQLiteAsyncConnection(path);  
  30.         }  
  31.     }  
  32. }  

For iOS, you have to do the same. Add a new class to the iOS project and the C# code is almost the same as the Android.

C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using Foundation;  
  7. using PracApp1.Connections;  
  8. using PracApp1.iOS;  
  9. using SQLite;  
  10. using UIKit;  
  11. using Xamarin.Forms;  
  12.   
  13. [assembly: Dependency(typeof(SqlConnection))]  
  14. namespace PracApp1.iOS  
  15. {  
  16.     class SqlConnection : ISqlConnection  
  17.     {  
  18.         public SQLiteAsyncConnection Connection()  
  19.         {  
  20.             var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  
  21.             var path = Path.Combine(documentsPath, "MySQLite.sqldb");  
  22.   
  23.             return new SQLiteAsyncConnection(path);  
  24.         }  
  25.     }  
  26. }  

For UWP also, do the same. Add a new class but the code is a bit different from the Android and iOS. You can use the following code.

C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using PracApp1.Connections;  
  8. using PracApp1.UWP;  
  9. using SQLite;  
  10. using Windows.Storage;  
  11. using Xamarin.Forms;  
  12.   
  13. [assembly: Dependency(typeof(SqlConnection))]  
  14.   
  15. namespace PracApp1.UWP  
  16. {  
  17.     class SqlConnection : ISqlConnection  
  18.     {  
  19.         public SQLiteAsyncConnection Connection()  
  20.         {  
  21.             string documentPath = ApplicationData.Current.LocalFolder.Path;  
  22.             string path = Path.Combine(documentPath, "MySQLite.sqldb");  
  23.   
  24.             return new SQLiteAsyncConnection(path);  
  25.         }  
  26.     }  
  27. }  

Summary

This article focused on accessing the SQLite database using Xamarin.Forms. So, the connection with the SQLite is successfully built. Now, you can operate with its functionalities, like CRUD functions and many others. Xamarin.Forms supports database-driven applications using the SQLite database engine, which makes it possible to load and save the objects in a shared code.


Similar Articles