Glory to Ukraine! 🇺🇦

Adding SQLite to the .NET MAUI application

Adding SQLite to the .NET MAUI application

11 December 2021

.NET MAUI/Xamarin

Almost every application needs to store data. Today I will show you how to connect SQLite to the .NET MAUI application.

First of all, we need to install NuGet packages:

<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.0" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.0" />
<PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.0" />
<PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.0" />

Then create repository class:

private readonly SQLiteConnection _database;

public Repository()
{
    var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "entities.db");
    _database = new SQLiteConnection(dbPath);
    _database.CreateTable<MyEntity>();
}

public List<MyEntity> List()
{
    return _database.Table<MyEntity>().ToList();
}

public int Create(MyEntity entity)
{
    return _database.Insert(entity);
}

public int Update(MyEntity entity)
{
    return _database.Update(entity);
}

public int Delete(MyEntity entity)
{
    return _database.Delete(entity);
}

It's up to you how you initialize the database and which methods to implement.

To use the repository update the MainPage:

private readonly Repository repository;

public MainPage()
{
    repository = new Repository();
    InitializeComponent();
}

protected override void OnAppearing()
{
    base.OnAppearing();
    GetEntities();
}

private void GetEntities()
{
    collectionView.ItemsSource = repository.List();
}

Important part for iOS

For iOS/MacCatalyst we need to set the SQLite provider. We can do it in AppDelegate:

protected override MauiApp CreateMauiApp()
{
    raw.SetProvider(new SQLite3Provider_sqlite3());
    return MauiProgram.CreateMauiApp();
}

.NET MAUI Sqlite

The full code can be found on GitHub.

The .NET MAUI Blazor sample can be found on GitHub.

The sample with EntityFramework Core can be found on GitHub.

Related:

How to show SnackBar and Toast using Xamarin Community Toolkit

Demonstrate how to configure SnackBar and Toast using Xamarin Community Toolkit.

Creating Kanban Board using Xamarin Forms 5

This article describes how to create Kanban Board using Xamarin Forms (.NET MAUI) only. Drag & Drop Cards, Set Column WIP, Store data in Local Db.

An error has occurred. This application may no longer respond until reloaded. Reload 🗙