The type initializer for 'SQLite.SQLiteConnection' threw an exception
Asked Answered
P

19

26

I'm trying to implement an incredibly basic use of SQLite. I have a Button and an EditText. I want to store the contents of the EditText OnClick.

I'm following this: https://developer.xamarin.com/guides/android/application_fundamentals/data/part_3_using_sqlite_orm/

and https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/

I cannot get passed the following starting code without getting the subsequent errors: var db = new SQLiteConnection (dbPath);

Error:

The type initializer for 'SQLite.SQLiteConnection' threw an exception.

Inner Exception:

System.Exception: This is the 'bait'. You probably need to add one of the SQLitePCLRaw.bundle_* nuget packages to your platform project.
at SQLitePCL.Batteries_V2.Init () [0x00000] in <9baed10c674b49e0b16322f238b8ecc1>:0 at SQLite.SQLiteConnection..cctor () [0x00000] in /Users/vagrant/git/src/SQLite.cs:169 }

I've installed the NuGet package on both PCL and Android projects. I see the following packages installed:

SQLitePCLRaw.provider.e_sqlite3.android
SQLitePCLRaw.lib.e_sqlite3.android

I've tried installing:

SQLitePCLRaw.bundle_e_sqlite3

As mentioned, the code is the most basic implementation possible:

try
{
    string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TestDB-DEV.db3");

    var db = new SQLiteConnection(dbPath);
    db.CreateTable<PersonName>();
}

I've spent a couple days on this and tried numerous resources like: https://forums.xamarin.com/discussion/87289/sqlite-net-pcl-bait-issue but ultimately no success.

Unfortunately, nonsense like "it just works", "not sure what I did", "clean/rebuild" are the only answers I've seen, e.g. previous link, other SO posts like Xamarin SQLite "This is the 'bait'"

Here is my package.config for the Android project:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="sqlite-net-pcl" version="1.4.118" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.bundle_green" version="1.1.5" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.core" version="1.1.5" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.lib.e_sqlite3.android" version="1.1.5" targetFramework="monoandroid60" />
  <package id="SQLitePCLRaw.provider.e_sqlite3.android" version="1.1.5" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.Animated.Vector.Drawable" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.Design" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.v4" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.v7.AppCompat" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.v7.CardView" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.v7.MediaRouter" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.v7.RecyclerView" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Android.Support.Vector.Drawable" version="23.3.0" targetFramework="monoandroid60" />
  <package id="Xamarin.Forms" version="2.4.0.282" targetFramework="monoandroid60" />
</packages>

Here is the package.config for the PCL project:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="sqlite-net-pcl" version="1.4.118" targetFramework="portable45-net45+win8+wpa81" />
  <package id="SQLitePCLRaw.bundle_green" version="1.1.5" targetFramework="portable45-net45+win8+wpa81" />
  <package id="SQLitePCLRaw.core" version="1.1.5" targetFramework="portable45-net45+win8+wpa81" />
  <package id="Xamarin.Forms" version="2.3.4.247" targetFramework="portable45-net45+win8+wpa81" />
</packages>
Portfire answered 24/10, 2017 at 16:16 Comment(3)
if you're using XF, you should follow the XF guide, not the Android one. See developer.xamarin.com/guides/xamarin-forms/…Postimpressionism
I actually started there. That was my first guide. I'm using the NuGet package shown in the guide.Portfire
I'm surprised I hadn't noticed this until now, but does 'monoandroid60' matter? My device is API24, so 'monoandroid70'. I guess I will install the updated SDK and see if that helps.Portfire
P
16

I hate to put myself in the "I don't know how I fixed it" boat, but that's what happened. I started clean and copy+pasted the code and repulled Nuget packages and everything just worked. Maybe I overlooked something initially, maybe had a version mismatch, I cannot say. However, I tried adding the dependencies mentioned by Trevor and the problem still existed, so I don't think I was missing anything.

Portfire answered 31/10, 2017 at 23:25 Comment(5)
The same... Spent 1 hour for ... nothingDiffluent
Had same problem when passed to Visual Studio 2019. I updated the nuget packages and all is good.Dummy
I have a logical answer here: https://mcmap.net/q/513411/-the-type-initializer-for-39-sqlite-sqliteconnection-39-threw-an-exceptionSerafina
I must say, I ended up exactly here too... I uninstalled all the Nuget packages, cleaned my build, and scrubbed all ephemeral files from folders manually, reinstalled sqlite3, recompiled, and bingo. (This is after a day of taking a 'scientific' approach and tackling things logically, following 'sensible' suggestions here and elsewhere)....Pedigree
My guess is this gave you fresh obj and bin foldersSweepstakes
S
30

Alright, it's been almost five years since you guys asked this question, and nobody seems to know how to fix the issue. I investigated it and found two workarounds. It's worth mentioning that I am using a .NET MAUI application and Visual Studio Community 2022.

  1. First, using NuGet packages: If you install the sqlite-net-pcl package with Install-Package sqlite-net-pcl -Version 1.8.116 in Visual Studio, it will install its main dependency SQLitePCLRaw.bundle_green using the version 2.0.4. Update that package to the latest version (2.1.2 as of this writing) with Install-Package SQLitePCLRaw.bundle_green. This process will uninstall the previous version. Now, save the Solution and close Visual Studio. Go to the project's folder and delete the folders bin and obj. Reopen Visual Studio, restore NuGet packages, build, and run.
  2. Second, if the above does not work, uninstall the package sqlite-net-pcl, clone the sqlite-net-pcl GitHub repository (https://github.com/praeclarum/sqlite-net), open your project, and add a reference to the SQLite-net-base project. Next, you must initialize a provider using the raw class: SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());. You must do this in the class that initializes your target platform. For instance, Android defines a MainApplication that inherits the Microsoft.Maui.MauiApplication class. In that MainApplication class, modify the CreateMauiApp() method like so:
using Android.App;
using Android.Runtime;

[Application]
public class MainApplication : MauiApplication
{
    public MainApplication(IntPtr handle, JniHandleOwnership ownership)
        : base(handle, ownership)
    {
    }

    protected override MauiApp CreateMauiApp()
    {
        SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
        return MauiProgram.CreateMauiApp();
    }
}

If you also use iOS and MacCatalyst (macOS) applications, modify the AppDelegate class like so:

For iOS:

using Foundation;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    protected override MauiApp CreateMauiApp()
    {
        SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
        return MauiProgram.CreateMauiApp();
    }
}

For MacCatalyst:

using Foundation;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    protected override MauiApp CreateMauiApp()
    {
        SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
        return MauiProgram.CreateMauiApp();
    }
}

Don't forget to always delete the bin and obj folders.

I hope this helps and good luck!

Shake answered 9/10, 2022 at 17:52 Comment(6)
This worked for me. Installed SQLitePCLRaw.bundle_green from 2.0.4 to 2.1.4 (latest version as at 05-Feb-2023). This automatically updated the bundle_green previously attached to sqlite-net-pcl.Quartas
The 1st. solution ( install SQLitePCLRaw.bundle_green 2.1.14 separately ) works for me to fix the sqlite init connection exception on iOS.Pulpboard
I would like to add that the first solution worked for me also. I had a slight variance in that I didn't have SQLitePCLRaw.bundle_green at all for some reason, yet my app would compile and work on Android perfectly fine and it missing only impacted iOS. (MAUI, .NET 6)Nutritious
The first option worked for me. I had upgraded from Xamarin Forms to MAUI and started receiveing this same exception as OP.Spiel
Just adding SQLitePCLRaw.bundle_green (which was missing completely) fixed it for me (VS2022CE Preview on Mac). ThanksMabellemable
add SQLitePCLRaw.bundle_green worked for me too. ThanksDeiform
P
16

I hate to put myself in the "I don't know how I fixed it" boat, but that's what happened. I started clean and copy+pasted the code and repulled Nuget packages and everything just worked. Maybe I overlooked something initially, maybe had a version mismatch, I cannot say. However, I tried adding the dependencies mentioned by Trevor and the problem still existed, so I don't think I was missing anything.

Portfire answered 31/10, 2017 at 23:25 Comment(5)
The same... Spent 1 hour for ... nothingDiffluent
Had same problem when passed to Visual Studio 2019. I updated the nuget packages and all is good.Dummy
I have a logical answer here: https://mcmap.net/q/513411/-the-type-initializer-for-39-sqlite-sqliteconnection-39-threw-an-exceptionSerafina
I must say, I ended up exactly here too... I uninstalled all the Nuget packages, cleaned my build, and scrubbed all ephemeral files from folders manually, reinstalled sqlite3, recompiled, and bingo. (This is after a day of taking a 'scientific' approach and tackling things logically, following 'sensible' suggestions here and elsewhere)....Pedigree
My guess is this gave you fresh obj and bin foldersSweepstakes
B
10

I fixed the same problem by downgrading the "sqlite-net-pcl" package to the last stable version (v1.7.302-beta → v1.6.292).

Bohlen answered 21/11, 2019 at 9:18 Comment(1)
I had this same issue today using the guide at learn.microsoft.com/en-us/xamarin/android/data-cloud/… I rolled back to latest stable and it fixed my issueAthamas
P
5

In my case, I got the error, because I was missing the "runtimes" folder in the "bin" folder of my Windows Forms application.

There should be a child-folder hierachy like this below the folder where your executable is stored:

  • runtimes\win-arm\native
  • runtimes\win-x64\native
  • runtimes\win-x86\native

In each folder, there is a "e_sqlite3.dll" file.

After I added those to the folder where my executable was located, the error went away. Actually, the files are automatically copied the the output folder if you use the appropriate SQLite NuGet packages.

Pectinate answered 21/1, 2020 at 12:3 Comment(0)
M
4

I had the same problem, deleting the "bin" and "obj" folders solved it.

Monopteros answered 21/4, 2019 at 21:54 Comment(1)
Same for me....Sweepstakes
H
3

I have resolved the issue by also installing the latest SQLitePCLRaw.core and SQLitePCLRaw.provider.dynamic_cdecl Packages from Nuget.

Haldeman answered 27/6, 2022 at 10:54 Comment(2)
This also worked for me, but an explanation as to why would be useful.Margheritamargi
For android this package is must. This is mentioned by Microsoft. learn.microsoft.com/en-us/training/modules/store-local-data/…Baines
C
3

My environment is Visual Studio 2022 Community Version 17.5.5 with MAUI App Template and .NET 7.0 Framework. I find out below Procedure for install sqlite-net-pcl.

1.install sqlite-net-pcl

2.quit visual studio and delete bin, obj folder

3.restart visual studio.

4.install SQLitePCLRaw.bundle_green

5.install SQLitePCLRaw.core

6.install SQLitePCLRaw.provider.dynamic_cdecl

7.install SQLitePCLRaw.provider.sqlite3

never use your project name with "sqlite-net-pcl".

in my case if stage 5 ~ 7 skip then no build error but code intelisense is not working, then it is recommended that you follow all steps.

Clutter answered 12/5, 2023 at 16:27 Comment(0)
P
2

I have solved the problem by downgrading the sql-net-pcl library from 1.7.302-beta to version 1.6.292

Photometer answered 7/3, 2020 at 10:49 Comment(0)
P
2

In my case, it was working fine for MAUI Windows however was failing for Android. I have installed SQLitePCLRaw.bundle_green 2.1.4 (Stable) via nuget and was able to successfully deploy it for Android.

Phlegmy answered 11/4, 2023 at 9:29 Comment(0)
C
1

Managed to SOLVE it. The thing was that I was using Nuget package in 2 different projects and I was updated only one of them. Alighting other project with the same version of SQLite Nuget and Clean, Rebuild solved the problem.

Crews answered 17/6, 2019 at 17:36 Comment(0)
D
1

Clean solution and then rebuild. If not working reinstall sqlite nuget. If still not working check versions of all nugets ef sqlite etc. (update them if you can)

Deberadeberry answered 19/3, 2020 at 18:30 Comment(1)
Clean solution, exit all instances of Visual Studio, then restart and try again and it works.Continuum
K
1

I ran into this when I tried to build an installer with VS Installer Projects for an app using sqlite-net-pcl. It ran fine when I launched it from VS, but the installed app crashed with this error message.

Turns out Installer Projects was not creating the file tree correctly. In the Application Folder, you need to add a folder named runtimes from your bin folder.

folder structure

Now when you build the installer and run it, the sql-lite dlls will be in the right place and you won't get this error.

Kersey answered 15/10, 2021 at 12:38 Comment(0)
C
1

I ended up fixing this by uninstalling Microsoft.Data.Sqlite.Core.

Cloudberry answered 8/1, 2022 at 12:57 Comment(0)
I
0

Install the following one package in all the projects (PCL, Android, iOS) Although, it is not being maintained but it works for me.

enter image description here

Then Write the following Platform Specific Code to get the Database Connection in each respective Platforms:

Android

public SQLiteConnection GetConnection()
{
     var dbName = "TestDB-DEV.db3";
     var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
     var path = Path.Combine(documentsPath, dbName);

     var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
     var connection = new SQLiteConnection(platform, path);

     return connection;
 }

iOS

public SQLiteConnection GetConnection()
{
    var dbName = "TestDB-DEV.db3";
    string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    string libraryPath = Path.Combine(folder, "..", "Library");
    var path = Path.Combine(libraryPath, dbName);
    var platform = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();
    var connection = new SQLiteConnection(platform, path);

    return connection;
}

You would be using the reference to the SQLite.Net as follow:

using SQLite.Net;

Hope this helps!

Irmgard answered 24/10, 2017 at 16:39 Comment(1)
This goes directly against all recommendations I've read on which Nuget Package to choose. Every example I've seen uses sqlite-net-pcl, not SQLite.Net PCL. With that said, your code isn't compatible with sqlite-net-pcl. However, I will test completely uninstalling sqlite-net-pcl and install SQLite.Net PCL if no better solutions present themselves.Portfire
S
0

It looks to me like you're still missing some dependencies in both the PCL and Android project. Here is a comparison of what I have in a working project.

Here is what I have in the PCL packages.config:

...
<package id="sqlite-net-pcl" version="1.4.118" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCL.bundle_green" version="0.9.3" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCL.raw" version="0.9.3" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.bundle_green" version="1.1.8" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.core" version="1.1.8" targetFramework="portable45-net45+win8+wpa81" />
...

Here is what I have in the Android packages.config:

...
<package id="sqlite-net-pcl" version="1.4.118" targetFramework="monoandroid71" />
<package id="SQLitePCL.bundle_green" version="0.9.3" targetFramework="monoandroid71" />
<package id="SQLitePCL.plugin.sqlite3.android" version="0.9.3" targetFramework="monoandroid71" />
<package id="SQLitePCL.raw" version="0.9.3" targetFramework="monoandroid71" />
<package id="SQLitePCLRaw.bundle_green" version="1.1.8" targetFramework="monoandroid71" />
<package id="SQLitePCLRaw.core" version="1.1.8" targetFramework="monoandroid71" />
<package id="SQLitePCLRaw.lib.e_sqlite3.android" version="1.1.8" targetFramework="monoandroid71" />
<package id="SQLitePCLRaw.provider.e_sqlite3.android" version="1.1.8" targetFramework="monoandroid71" />
...
Sirmons answered 25/10, 2017 at 14:32 Comment(1)
Interesting, I will add those missing packages and check back.Portfire
P
0

I had the issue of 'SQLite.SQLiteConnection' exception when running on iOS. I installed the Beta Version of sqlite-net-pcl version 1.9.141-beta, it worked perfectly on a MAUI app.

Porche answered 21/2, 2023 at 21:46 Comment(0)
C
0

Run it with specific version for me on linux this worked

dotnet run --runtime win-x86

Hope this helps!

Casein answered 27/5, 2023 at 17:6 Comment(0)
D
0

uninstall packages sqlite-net-pcl and SQLitePCLRaw.bundle_green and install again

Dab answered 31/10, 2023 at 16:19 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Caprine
I
0

Just add this Nuget package to your MAUI project.

SQLitePCLRaw.bundle_green

Indehiscent answered 30/1, 2024 at 13:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.