1

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 :

  1. Xamarin version -> 4.6.0.726
  2. The strategy of code sharing -> .NET standard
  3. 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

  • if your shared project is .NET Standard your package references should not look like that. With the newest sqlite nuget you should also not need to use DependencyService to create your connection. See https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database – Jason May 12 '20 at 23:21
  • @Jason I followed that tutorial, and I choose the .NET standard option given by the wizard when you are going to create a new project. What do you think I could be missing? – Jesé Abraham Chavez Rivas May 12 '20 at 23:30
  • that tutorial doesn't use DependencyService, why are you? Open the project options dialog for the shared project and verify that it's .NET Standard. Also try deleting and re-adding the nuget packages – Jason May 12 '20 at 23:32
  • @Jason I already tried deleting and readding the NuGet package. Well, my VS2017 say it's .NET standard, I also did a new project with the same configuration, and is giving me the same problem. – Jesé Abraham Chavez Rivas May 12 '20 at 23:37
  • @JeséAbrahamChavezRivas I test the code without dependency service and add the code `x.CreateTableAsync().Wait();` to create the the table before `await x.Table().ToListAsync();` in `try` statement. It works well on my PC. I could not reproduce the error, could you provide more details? Do you download the sample code provided bu Jason with the link: https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows Does this throw the same exception? – Wendy Zang - MSFT May 13 '20 at 05:26
  • @WendyZang-MSFT Well, the sample code works perfectly, and I'm using the same structure(not taking in count dependency service ), I'm going to put my GitHub repository, there's only a branch, and the one commit working is the one before the last.Thanks https://github.com/jesse0099/Hackathon2019-Movil.git – Jesé Abraham Chavez Rivas May 13 '20 at 21:10
  • @JeséAbrahamChavezRivas Due to some errors, i could not test the code you provided. But for the code sample, do not thrown the same error with yours. Could you provide some details about which line of the code thrown the error or a simple example which can reproduce the error for me? – Wendy Zang - MSFT May 18 '20 at 06:58
  • @WendyZang-MSFT The connection itself comes with the exception, but this is thrown until I call GetItemsAsync.I put my GitHub repository at the end of the question, you could reproduce the problem cloning it, thanks – Jesé Abraham Chavez Rivas May 18 '20 at 20:47

1 Answers1

1

What fixed the issue for me was removing all of the SQLitePCLRaw packages from my project, and just keeping sql-net-pcl. This worked in MAUI.

3030tank
  • 99
  • 2
  • 10