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