Press ESC to close

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.

using SQLite.Net;
using SQLite.Net.Platform.XamarinAndroid;
using VeriTabani.Droid;

[assembly: Xamarin.Forms.Dependency(typeof(DroidBaglantisi))]
namespace VeriTabani.Droid
{
    public class DroidBaglantisi : SqlConnection
    {
        public SQLiteConnection BaglantiyiAl()
        {
            string yol = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var yol2 = System.IO.Path.Combine(yol, App.VTAdi);
            var platform = new SQLitePlatformAndroid();

            var baglanti = new SQLiteConnection(platform, yol2);
            return baglanti;
        }
    }
}

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.

using SQLite.Net.Attributes;
using System;

namespace VeriTabani
{
    public class Kullanici
    {
        [PrimaryKey,AutoIncrement]
        public int ID { get; set; }
        public string Ad { get; set; }
        public string Soyad { get; set; }
        public DateTime KayitTarihi { get; set; }

    }
}

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.

using SQLite.Net;
using System.Collections.Generic;
using Xamarin.Forms;

namespace VeriTabani
{
    public class Yonetici
    {
        SQLiteConnection veritabaniBaglantisi;
        public Yonetici()
        {
            veritabaniBaglantisi = DependencyService.Get<SqlConnection>().BaglantiyiAl();
            veritabaniBaglantisi.CreateTable<Kullanici>();
        }
        public int Kaydet(Kullanici model)
        {
            return veritabaniBaglantisi.Insert(model);
        }

        public int Guncelle(Kullanici model)
        {
            return veritabaniBaglantisi.Update(model);
        }
        public int Sil(Kullanici model)
        {
            // Birden fazla talo olabilir bu yüzden hangi tablodaysa ona göre belirtmemiz gerekiyor.
            return veritabaniBaglantisi.Delete<Kullanici>(model);
        }
        public IEnumerable<Kullanici> Listele()
        {
            return veritabaniBaglantisi.Table<Kullanici>();
        }
        public Kullanici Getir(int ID)
        {
            return veritabaniBaglantisi.Table<Kullanici>().Where(x => x.ID == ID).FirstOrDefault();
        }

        public void Kapat()
        {
            veritabaniBaglantisi.Dispose();
        }
    }
}

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

Leave a Reply

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