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.
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);
}
}
}