Xamarin Storing Data inLocal (SQLite)

Your mobile applications may be small in size. You may only want to store your data locally, or you may want to store it in the cloud as well as in a local location. For that, Xamarin again offers us this very easily.

As always, we create our project first. I choose portable. The first thing we will do next is to update the Xamarin.Forms package. After updating, we clean and rebuild the project. We come back to the packages section and load all the layers of the Sqlite.net-pcl package. We need to connect to each platform separately after the upload is finished. We can do this with dependency services. Already in previous writings. To do this, we first create an interface at the portable layer. We then create a class for each platform to get the connection information.

Let’s start with the Android platform. I need to create a connection first. We need to open a class and inherit the interfacial inheritance we created in the portable part of it. After taking inheritance and implementing it, we need to establish a connection. To create a link, we need to tell which platform it is on and what path to use. We need to combine the path with the name of our database while giving it. Instead of creating separate database names for each platform, I define a name in the App class and I combine it. First I specify the path. Then we’ll work on the android platform, so we assign a variable and link it. This link is finally returned to the function. We also specify that this class is a Dependency and we are now finishing our work here.

After creating the connection, we create a model according to which database object we want to process on. This model can also use the features of Sqlite. Features such as Primary Key, auto-increment. I will create a user model and process accordingly.

We need an administrator class to perform operations such as adding, deleting, updating, reading on our database. I open this on the portable layer and write our actions. You can do all kinds of things with the object from here.

If you have a problem, you can mail or comment.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.