23

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>
3
  • if you're using XF, you should follow the XF guide, not the Android one. See developer.xamarin.com/guides/xamarin-forms/…
    – Jason
    Oct 24, 2017 at 16:18
  • I actually started there. That was my first guide. I'm using the NuGet package shown in the guide. Oct 24, 2017 at 16:45
  • 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. Oct 24, 2017 at 17:47

17 Answers 17

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.

4
  • The same... Spent 1 hour for ... nothing
    – Arnaud F.
    Dec 23, 2017 at 16:42
  • Had same problem when passed to Visual Studio 2019. I updated the nuget packages and all is good.
    – qfactor77
    May 18, 2019 at 12:30
  • I have a logical answer here: stackoverflow.com/a/56636305/4404849 Jun 27, 2019 at 5:22
  • 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)....
    – Edward
    Jul 12, 2021 at 18:28
16

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!

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. Feb 4 at 23:22
  • The 1st. solution ( install SQLitePCLRaw.bundle_green 2.1.14 separately ) works for me to fix the sqlite init connection exception on iOS. Feb 10 at 3:05
  • 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)
    – Laim
    Feb 14 at 22:44
  • The first option worked for me. I had upgraded from Xamarin Forms to MAUI and started receiveing this same exception as OP.
    – kRiZ
    Jun 27 at 5:06
  • Just adding SQLitePCLRaw.bundle_green (which was missing completely) fixed it for me (VS2022CE Preview on Mac). Thanks Jul 18 at 9:58
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).

1
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.

3

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

2

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

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.

0
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)

1
  • Clean solution, exit all instances of Visual Studio, then restart and try again and it works.
    – SqueezeOJ
    May 6 at 21:09
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.

1

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

1

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

1
  • This also worked for me, but an explanation as to why would be useful.
    – devonuto
    Sep 1, 2022 at 5:04
1

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.

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" />
...
1
  • Interesting, I will add those missing packages and check back. Oct 25, 2017 at 19:13
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!

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. Oct 24, 2017 at 16:50
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.

0

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.

0

Run it with specific version for me on linux this worked

dotnet run --runtime win-x86

Hope this helps!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.