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.
Select "Browse" and search for sqlite-net-pcl and install it in all projects.
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.
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.
Add the following code in interface.
- using SQLite;
-
- namespace XamarinApp1.Persistence
- {
- public interface ISQLiteDb
- {
- SQLiteAsyncConnection GetConnection();
- }
- }
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
- using System;
- using System.IO;
- using SQLite;
- using XamarinApp1.Persistence;
- using XamarinApp1.Droid.Persistence;
- using Xamarin.Forms;
-
- [assembly: Dependency(typeof(SQLiteDb))]
-
- namespace XamarinApp1.Droid.Persistence
- {
- public class SQLiteDb : ISQLiteDb
- {
- public SQLiteAsyncConnection GetConnection()
- {
- var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- var path = Path.Combine(documentsPath, "MySQLite.db3");
-
- return new SQLiteAsyncConnection(path);
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.Text;
- using SQLite;
- using XamarinApp1.Persistence;
- using System.IO;
- using Xamarin.Forms;
- using XamarinApp1.iOS.Persistence;
-
- [assembly: Dependency(typeof(SQLiteDb))]
-
- namespace XamarinApp1.iOS.Persistence
- {
- public class SQLiteDb : ISQLiteDb
- {
- public SQLiteAsyncConnection GetConnection()
- {
- var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- var path = Path.Combine(documentsPath, "MySQLite.db3");
-
- return new SQLiteAsyncConnection(path);
- }
- }
- }
For Windows
For Windows, there is a change in path of db.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SQLite;
- using XamarinApp1.Persistence;
- using Windows.Storage;
- using System.IO;
- using Xamarin.Forms;
-
- [assembly: Dependency(typeof(XamarinApp1.UWP.Persistence.SQLiteDb))]
-
- namespace XamarinApp1.UWP.Persistence
- {
- public class SQLiteDb : ISQLiteDb
- {
- public SQLiteAsyncConnection GetConnection()
- {
- string documentPath = ApplicationData.Current.LocalFolder.Path;
- string path = Path.Combine(documentPath, "MySQLite.db3");
-
- return new SQLiteAsyncConnection(path);
- }
- }
- }
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.