13

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.

1
  • The error happens on real Android device or Android emulators?Have you installed all the necessary nuget packages?Please create a Minimal, Reproducible Example via github repo. Commented Jun 27, 2022 at 6:44

3 Answers 3

19

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);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

both SQLiteAsyncConnection and SQLiteConnection give me runtime errors.
For a week I've been looking for a solution, turns out it was just adding a few more packages... Thanks a lot for making my day!
This just solved all my problems with SQLite not starting up Thanks !!!
2

I installed the package SQLitePCLRaw.provider.dynamic_cdecl and it worked.

Comments

1

I experienced the same exception.

It was caused by having spaces in the Project Name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.