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

浪子不回头ぞ 提交于 2020-03-18 11:36:53

问题


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>

回答1:


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.




回答2:


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




回答3:


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




回答4:


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.




回答5:


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" />
...



回答6:


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.




回答7:


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

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!




回答8:


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



来源:https://stackoverflow.com/questions/46915404/the-type-initializer-for-sqlite-sqliteconnection-threw-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!