7

db = new SQLiteConnection(databasePath);

The above statement works fine on Windows but gets the below error on Android. It also worked fine in xamarin forms on both platforms.

System.IO.FileNotFoundException: 'Could not load file or assembly 'SQLitePCLRaw.provider.dynamic_cdecl, Version=2.0.4.976, Culture=neutral, PublicKeyToken=b68184102cba0b3b' or one of its dependencies.'

Thanks for any help.

Jim
  • 73
  • 3
  • The error happens on real Android device or Android emulators?Have you installed all the necessary nuget packages?Please create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) via github repo. – Alexandar May - MSFT Jun 27 '22 at 06:44

2 Answers2

13

There are a few things that you need take care of when using sqlite in your MAUI project.

1.Make sure you have installed below nuget packages:

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

2.Use SQLiteAsyncConnection to initialize the database connection.

Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);

And then create static class Constants like below:

 public static class Constants
    {
        public const string DatabaseFilename = "MySQLite.db3";
        public const SQLite.SQLiteOpenFlags Flags =
            SQLite.SQLiteOpenFlags.ReadWrite |
            SQLite.SQLiteOpenFlags.Create |
            SQLite.SQLiteOpenFlags.SharedCache;
        public static string DatabasePath
        {
            get
            {
                var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                return Path.Combine(basePath, DatabaseFilename);
            }
        }
    }
Alexandar May - MSFT
  • 6,536
  • 1
  • 8
  • 15
1

I experienced the same exception.

It was caused by having spaces in the Project Name

Ritchie B
  • 75
  • 8