Hello,i'm trying to use SQLite in a Xamarin Forms project.But i'm getting a hard time making it work.I'm going to give information about my project configuration :
- Xamarin version -> 4.6.0.726
- The strategy of code sharing -> .NET standard
- sqlite-net-pcl 1.6.292
Problem : The following exception is thrown when I try to obtain a connection -> {system.typeinitializationexception: the type initializer for 'sqlite.sqliteconnection' threw an exception. ---> system.dllnotfoundexception: e_sqlite3 at (wrapper managed-to-native) sqlitepcl.sqlite3provider_e_sqlite3+nativemethods.sqlite3_libversion_number() at sqlitepcl.sqlite3provider_e_sqlite3.sqlitepcl.isqlite3provider.sqlite3_libversion_number () [0x00000] in :0 at sqlitepcl.raw.setprovider (sqlitepcl.isqlite3provider imp) [0x00008] in :0 at sqlitepcl.batteries_v2.init () [0x00005] in :0 at sqlite.sqliteconnection..cctor () [0x00016] in <84b9c9e630fa45bd8ac799333976ebbf>:0 --- end of inner exception stack trace --- at sqlite.sqliteconnectionwithlock..ctor (sqlite.sqliteconnectionstring connectionstring) [0x0000b] in <84b9c9e630fa45bd8ac799333976ebbf>:0 at sqlite.sqliteconnectionpool+entry.connect () [0x0001c] in <84b9c9e630fa45bd8ac799333976ebbf>:0 at sqlite.sqliteconnectionpool.getconnection (sqlite.sqliteconnectionstring connectionstring) [0x00048] in <84b9c9e630fa45bd8ac799333976ebbf>:0 at sqlite.sqliteasyncconnection.getconnection () [0x00005] in <84b9c9e630fa45bd8ac799333976ebbf>:0 at sqlite.sqliteasyncconnection.get_tracer () [0x00000] in <84b9c9e630fa45bd8ac799333976ebbf>:0 }
I have done intensive research and I found nothing that works for me.The last post that I visited was this : Android - The type initializer for 'SQLite.SQLiteConnection' threw an exception. ---> System.DllNotFoundException: e_sqlite3 the accepted answer just says that installing the nugget on android project will solve the problem(Thing that I already did)
I'll show some code and how my nuggets look. Droid packages
<package id="sqlite-net-pcl" version="1.6.292" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.bundle_green" version="2.0.1" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.core" version="2.0.1" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.lib.e_sqlite3" version="2.0.1" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.lib.e_sqlite3.osx" version="1.1.14" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.provider.e_sqlite3" version="2.0.1" targetFramework="monoandroid90" />
Shared code project packages
<package id="sqlite-net-pcl" version="1.6.292" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.bundle_green" version="1.1.13" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.core" version="1.1.13" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.lib.e_sqlite3.osx" version="1.1.14" targetFramework="portable45-net45+win8+wpa81" />
Interface used to obtain path
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UISampleApp.Interfaces
{
public interface ISQLitePlatform
{
SQLiteConnection GetConnection();
SQLiteAsyncConnection GetConnectionAsync();
String GetPath();
}
}
Droid implementation
using System;
using System.IO;
using Android.OS;
using SQLite;
using SQLitePCL;
using UISampleApp.Interfaces;
using UISampleApp.Models;
[assembly: Xamarin.Forms.Dependency(typeof(UISampleApp.Droid.Implementations.SQLitePlatform))]
namespace UISampleApp.Droid.Implementations
{
public class SQLitePlatform : ISQLitePlatform
{
public string GetPath() {
var dbName =Constantes.DatabaseFileName;
var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),dbName);
return path;
}
public SQLiteConnection GetConnection()
{
return new SQLiteConnection(GetPath());
}
public SQLiteAsyncConnection GetConnectionAsync()
{
return new SQLiteAsyncConnection(GetPath());
}
}
}
Code generating the exception
string path = DependencyService.Get<ISQLitePlatform>().GetPath();
SQLite.SQLiteAsyncConnection x = DependencyService.Get<ISQLitePlatform>().GetConnectionAsync();
try
{
await x.Table<TodoItem>().ToListAsync();
}
catch (Exception e)
{
throw e;
}
I hope somebody could help me if more information is needed, just tell me and I'll provide it. PS: Doing the same on a project with PCL's sharing code strategy work, but I need this being working in a project with .NET standard as sharing code strategy.Thanks
GitHub: https://github.com/jesse0099/Hackathon2019-Movil.git