Use Sqllite at Xamarin.Form

2017/3/312 min read
bookmark this
Responsive image

I want to save my anroid, iOS app's information to local device with SQLite at Xamarin.Form. This blog provides how to set up an Enviroment to use SQLite.

Install Nuget package for SQLite

Install nuget package SQLite.Net-PCL, Xamarin.Form, Android and iOS projects. Also, when run the previous SQLite.Net-PCL will install SQLite.Net.Core-PCL as well.

<package id="SQLite.Net.Core-PCL" version="3.1.1" targetFramework="monoandroid71" />
  <package id="SQLite.Net-PCL" version="3.1.1" targetFramework="monoandroid71" />

Xamarin Form Project's shared Interface

First, let's define the interface at Xamarin Form project, so we could use dependency Service to implement device specific logic. At following example, we want to get the SQLiteConnection, however Android and iOS are different to setup the SQLiteConnection.

using SQLite.Net;

namespace YourXamarinFormProject.Interfaces
{
    public interface IDatabaseConnection
    {
       SQLiteConnection DbConnection();
    }
}

Xamarin Android code for SQLite

This code is just getting create SQLite connection at Xamarin Android by using Xmarin's DependencyService.

using YourXamarinAndroidApp ;
using SQLite.Net;
using System.IO;

[assembly: Xamarin.Forms.Dependency(typeof(DatabaseConnectionService))]
namespace YourXamarinAndroidApp .Services
{
    public class DatabaseConnectionService : IDatabaseConnection
    {
        public SQLiteConnection DbConnection()
        {
            var dbName = DbConst.SQLITE_DB_NAME;
            var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), dbName);
            return new SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), path);
        }
    }
}

Xamarin iOS code for SQLite

This code is just getting create SQLite connection at Xamarin iOS by using Xmarin's DependencyService.

using YouXamariniOSProject;
using SQLite.Net;
using System;
using System.IO;

[assembly: Xamarin.Forms.Dependency(typeof(DatabaseConnectionService))]
namespace YouXamariniOSProject.Services
{
    public class DatabaseConnectionService : IDatabaseConnection
    {
        public SQLiteConnection DbConnection()
        {
            var dbName = DbConst.SQLITE_DB_NAME;
            string personalFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string libraryFolder = Path.Combine(personalFolder, "..", "Library");
            var path = Path.Combine(libraryFolder, dbName);
            return new SQLiteConnection(new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS(), path);
        }
    }
}

Use SQLite at Xamarin.Form project

 

public class YouModelToSaveToSQLite
{
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public double Price { get; set; }
}
// create table
_db = DependencyService.Get().DbConnection();
db.CreateTable();

// insert the table
_db.Insert(YouModelToSaveToSQLite);

// delete all
_db.DeleteAll();

// get all records
_db.Table().ToList();

Reference Sites