ericsink / SQLitePCL.raw

A Portable Class Library (PCL) for low-level (raw) access to SQLite
Apache License 2.0
512 stars 106 forks source link

System.DllNotFoundException: 'e_sqlite3' #499

Closed EmilyEdna closed 1 year ago

EmilyEdna commented 1 year ago

I am using sqlite for Android development, but I get an error System.DllNotFoundException: 'e_sqlite3' and my environment is vs2022 Preview 2 version number is 17.3.0, and I am using MAUI, is there any good alternative? Thank you

ericsink commented 1 year ago

Can you post a minimal repro project?

adamjhilton commented 1 year ago

Hello! I'm running into the same issue. I'm migrating one of our mobile apps from Xamarin Forms to MAUI and can't get it to run on Android without getting this runtime error. It runs on Windows just fine. I have not tried it on iOS or OSX.

I had posted this issue in the MAUI issues, but was redirected here. You can read my fuller description of the issue here: https://github.com/dotnet/maui/issues/9651

Basically, in Xamarin Forms, I just had these NuGet packages installed and everything worked.

  <ItemGroup>
    <PackageReference Include="Simple.Migrations" Version="0.9.21" />
    <PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
  </ItemGroup>

In MAUI, I had a running list of runtime errors that kept me installing more and more packages before I got to the System.DllNotFoundException: 'e_sqlite3' that I couldn't get rid of. Also, that is what this post suggested: https://stackoverflow.com/questions/72755874/getting-a-filenotfoundexception-when-trying-to-make-a-sqlite-connection-in-net

    <ItemGroup>
      <PackageReference Include="Simple.Migrations" Version="0.9.21" />
      <PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
      <PackageReference Include="SQLiteNetExtensions" Version="2.1.0" />
      <PackageReference Include="SQLiteNetExtensions.Async" Version="2.1.0" />
      <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.0" />
      <PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.0" />
      <PackageReference Include="SQLitePCLRaw.core" Version="2.1.0" />
      <PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.1.0" />
      <PackageReference Include="SQLitePCLRaw.lib.e_sqlite3.osx" Version="1.1.14" />
      <PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.0" />
      <PackageReference Include="SQLitePCLRaw.provider.e_sqlite3" Version="2.1.0" />
    </ItemGroup>

As per a suggestion on the MAUI forum, I created a Android .NET 6.0 project (not MAUI) and am experiencing the same issue. So, I think it's an issue either in .NET 6.0 or in how it interacts with the Sqlite package.

I uploaded my .NET 6.0 Android app here :https://github.com/adamjhilton/AndoidTestAppForSqlite I'm running it on an Android 11.0 - API 30 emulated tablet. An exception will get thrown on line 15 of MainActivity.cs.

Thanks!

ericsink commented 1 year ago

sqlite-net-pcl 1.8.116 references SQLitePCLRaw.bundle_green 2.0.4, which is incompatible with MAUI.

My suggestion:

  1. Remove all those extra packages you added.
  2. Add SQLitePCLRaw.bundle_green 2.1.0.
adamjhilton commented 1 year ago

Thanks for the quick reply!

I uninstalled all packages except for SQLitePCLRaw.bundle_green. image

Now I'm getting a compiler error: image

ericsink commented 1 year ago

Sorry, I should have been more clear. You still need sqlite-net-pcl. I was just explaining how to update the bundle_green dependency underneath sqlite-net.

adamjhilton commented 1 year ago

Ah ha! I re-added the sqlite-net-pcl package. So, now I just have those two packages in the .NET 6.0 Android test app. It compiles but at runtime I still get: image

But I guess the bigger issue is that SQLite is incompatible with MAUI. Are there any plans to implement compatibility?

ericsink commented 1 year ago

I'm still explaining things in too much of a hurry. To clarify:

SQLite itself is compatible with MAUI.

SQLitePCLRaw is my low-level .NET wrapper around SQLite. sqlite-net is built on it.

SQLitePCLRaw 2.0.x is not compatible with MAUI.

but SQLitePCLRaw 2.1.x IS compatible with MAUI.

So, if you are using the latest sqlite-net-pcl and you have updated SQLitePCLRaw.bundle_green to 2.1.0, then that should work. The error you are seeing is not expected. So either I have a bug, or something else is going on.

I will take a look at your repro project and get back to you.

adamjhilton commented 1 year ago

You're fine! I'm just appreciating the responsiveness. Thank you.

Okay. I think I'm understanding this now. Do I have the wrong version of "sqlite-net-pcl" installed? I'm not seeing a v2.1 like your low-level packages. image

I've updated the repo with your package suggestions. So, you can take a look at it at your convenience.

ericsink commented 1 year ago

sqlite-net-pcl 1.8.116 is fine. It's the latest version, as far as I know. sqlite-net is a separate project, and I don't maintain it. I only maintain the SQLitePCLRaw layer underneath it.

ericsink commented 1 year ago

I grabbed your sample repo and, well, it works fine for me. :-(

To be sure, I added a line to execute a SQLite statement after you open the connection. My code looks like this:

    SQLiteConnection databaseConnection = new SQLiteConnection(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "registers.db3"));
    var s = databaseConnection.ExecuteScalar<string>("SELECT sqlite_version()");
    // set a breakpoint here

And if I set a breakpoint on something just after my ExecuteScalar call, I can see that s contains the version string of the SQLite library. So e_sqlite3 is getting properly deployed and loaded.

FWIW, I'm on Visual Studio 17.3.2.

adamjhilton commented 1 year ago

Well, that is... something. So, it must be something configurational on my machine. I checked for updates in VS and I'm up to date. Anything jump out at you in my About Microsoft Visual Studio dump below?

Microsoft Visual Studio Professional 2022 Version 17.3.2 VisualStudio.17.Release/17.3.2+32819.101 Microsoft .NET Framework Version 4.8.04084

Installed Version: Professional

Visual C++ 2022 00476-80000-00000-AA945 Microsoft Visual C++ 2022

ASP.NET and Web Tools 17.3.376.3011 ASP.NET and Web Tools

Azure App Service Tools v3.0.0 17.3.376.3011 Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 17.3.376.3011 Azure Functions and Web Jobs Tools

C# Tools 4.3.0-3.22412.4+c97184bafab9a34d61e85f1c1ef34f25283ce9ba C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio 17.3.20 (3f4cb00) Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 6.3.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Razor (ASP.NET Core) 17.0.0.2232702+e1d654e792aa2fe6646a6935bcca80ff0aff4387 Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools 17.0.62207.04100 Microsoft SQL Server Data Tools

StylerPackage Extension 1.0 StylerPackage Visual Stuido Extension Detailed Info

TypeScript Tools 17.0.10701.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 4.3.0-3.22412.4+c97184bafab9a34d61e85f1c1ef34f25283ce9ba Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 17.1.0-beta.22363.4+1b94f89d4d1f41f20f9be73c76f4b229d4e49078 Microsoft Visual F# Tools

Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio.

Visual Studio Spell Check Everywhere VSSpellCheckEverywhere An extension that enables spell checking within any Visual Studio file editor or tool window that uses WPF text boxes. https://GitHub.com/EWSoftware/VSSpellChecker

Visual Studio Spell Checker VSSpellChecker An editor extension that checks the spelling of comments, strings, and plain text as you type or interactively with tool windows. https://GitHub.com/EWSoftware/VSSpellChecker

VisualStudio.DeviceLog 1.0 Information about my package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info

Xamarin 17.3.0.296 (d17-3@a0ff24b) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 17.3.0.208 (remotes/origin/d17-3@e4f67afef) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 17.2.28 (9b3d244) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 13.0.0.0 (d17-3/030cd63) Xamarin.Android Reference Assemblies and MSBuild support. Mono: dffa5ab Java.Interop: xamarin/java.interop/d17-3@7716ae53 SQLite: xamarin/sqlite/3.38.5@df4deab Xamarin.Android Tools: xamarin/xamarin-android-tools/main@14076a6

Xamarin.iOS and Xamarin.Mac SDK 15.12.0.2 (87f98a75e) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

ericsink commented 1 year ago

Nothing weird there.

Have you tried forcing a clean full rebuild?

adamjhilton commented 1 year ago

Boom! That was it. Fixing all the package references, cleaning and then rebuilding was the ticket.

I'm up and running. Thank you!

EmilyEdna commented 1 year ago

This method is very effective, thank you very much for your reply

marwalsch commented 1 year ago

So, if you are using the latest sqlite-net-pcl and you have updated SQLitePCLRaw.bundle_green to 2.1.0, then that should work. The error you are seeing is not expected. So either I have a bug, or something else is going on.

This solution fixes the issue on iOS as well.