SQLite Database Not Loading on iOS and Android with .NET MAUI but works with windows

martin schneeberger 20 Reputation points
2025-03-24T10:19:17.6133333+00:00

Using Visual Studio 2022 preview with .NET 9 MAUI, there is an issue getting SQLite to work on iOS and Android. The code compiles for all maui platforms but does not load on the Android emulator or attached Iphone. It works on Windows without issues.

I am confusion regarding where to place the SQLite database for iOS and Android.

I am confused about what path to use for iOS and Android

Below is the relevant code:

using SQLite;
using System.IO;

public class Pointsdatabase
{
    public const string DatabaseName = "pointsdatabase.db";
    public const string TableName = "VelocityCreditCards";
    public const string csvpath =  C:\\Users\\schne\\source\\repos\\MPointsSQLite\\VelocityCreditCards.csv";
    public readonly string _databasePath;

    public Pointsdatabase() 
    {
        var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        _databasePath = Path.Combine(folderPath, DatabaseName);

        if (!File.Exists(DatabaseName))
        { 
            InitializeDatabase();
        }
    }

    private void InitializeDatabase()
    {
        using (var connection = new SQLiteConnection(_databasePath))
        {
            var command = connection.CreateCommand($@"
            CREATE TABLE IF NOT EXISTS {TableName} (
                Id INTEGER PRIMARY KEY AUTOINCREMENT,
                CreditCard TEXT,
                Own TEXT, 
                Myer TEXT,
                DavidJones TEXT,
                Eleven TEXT,
                Coles TEXT,
                Woolworths TEXT,
                BP TEXT
            )");
            command.ExecuteNonQuery();

            var countCommand = connection.CreateCommand($@"
            SELECT COUNT(*) FROM {TableName}");
            var count = countCommand.ExecuteScalar
Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-03-24T16:26:56.3433333+00:00

    you should use the Filesysten Helper, "FileSystem.Current.AppDataDirectory", not special folders.

    note: your csv path is not valid for non-windows.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.