Reputation: 1560
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>
Upvotes: 27
Views: 39902
Reputation: 445
I was converting my project from using SQLite "manually" to using EFCore. I still had package references for SQLitePCLRaw.core
and System.Data.SQLite.Core
which once I removed, deleted the bin/obj folders and rebuilt my dotnet ef migrations add InitialCreate
command worked.
Upvotes: 0
Reputation: 436
Just add this Nuget package to your MAUI project.
SQLitePCLRaw.bundle_green
Upvotes: 0
Reputation: 1
uninstall packages sqlite-net-pcl and SQLitePCLRaw.bundle_green and install again
Upvotes: 0
Reputation: 1
Run it with specific version for me on linux this worked
dotnet run --runtime win-x86
Hope this helps!
Upvotes: 0
Reputation: 41
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.
Upvotes: 4
Reputation: 153
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.
Upvotes: 2
Reputation: 1
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.
Upvotes: 0
Reputation: 991
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.
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.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!
Upvotes: 30
Reputation: 31
I have resolved the issue by also installing the latest SQLitePCLRaw.core and SQLitePCLRaw.provider.dynamic_cdecl Packages from Nuget.
Upvotes: 3
Reputation: 43
I ended up fixing this by uninstalling Microsoft.Data.Sqlite.Core.
Upvotes: 1
Reputation: 11
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.
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.
Upvotes: 1
Reputation: 21
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)
Upvotes: 1
Reputation: 671
I have solved the problem by downgrading the sql-net-pcl library from 1.7.302-beta to version 1.6.292
Upvotes: 2
Reputation: 40756
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:
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.
Upvotes: 5
Reputation: 2604
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!
Upvotes: 0
Reputation: 1070
I had the same problem, deleting the "bin" and "obj" folders solved it.
Upvotes: 4
Reputation: 2279
I fixed the same problem by downgrading the "sqlite-net-pcl" package to the last stable version (v1.7.302-beta → v1.6.292).
Upvotes: 10
Reputation: 136
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.
Upvotes: 1
Reputation: 1560
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.
Upvotes: 16
Reputation: 3898
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" />
...
Upvotes: 0