Implementing a local database in MAUI project
.NET MAUI is a multi-platform framework allowing you to build native, cross-platform, desktop and mobile application all in the same framework.
While working on some projects, you may encounter the need of storing data locally. I had this problem while developing my latest app, AudioMood, this app needs to store some data locally as I don’t have (still) an external server where to store data.
So I started storing data in a json file, but then I discovered the SQLite nuget package.
The SQLite nuget package
There are many sqlite nuget packages out there, but I decided to go with the official one: sqlite-net-pcl by sqlite-net.
This a minimal library working with .NET, .NET Core and Mono. It allows you to store data in an SQLite 3 local database.
Create your constants
Start by creating a Constants class used to store your database settings. Each database will be class-specific.
public static class Constants
{
public const string DatabaseFilename = "TodoSQLite.db3";
public const SQLite.SQLiteOpenFlags Flags =
// open the database in read/write mode
SQLite.SQLiteOpenFlags.ReadWrite |
// create the database if it doesn't exist…