skip to Main Content

So I am currently trying to develop a MAUI app using the sqlite-net-pcl nugget package. My app works fine in a Windows(Win11) enviroment, but throw an exception when I am using it on an Android device(Android 12).

To sum up the porpuse of this app is that I have a list of something(food recepies) and I want to store them in a persistance database, that’s why I am using sqlite for.

The "init" code that generates the db and the connection for it is this:

public static class BookPageService
{
   static SQLiteAsyncConnection db;
   static async Task Init()
    {
        if(db != null)
        {
            return;
        }

        // Get an absolute path to the database file
        //Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        //Xamarin.Essentials.FileSystem.AppDataDirectory
        var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyData.db");

        try
        {
            db = new SQLiteAsyncConnection(databasePath);// Get an absolute path to the database file
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        

        await db.CreateTableAsync<BookPage>();
    }

    public static async Task AddBookPage(string title, string description)
    {
        await Init();
        BookPage bookPage = new BookPage(title, description);
        await db.InsertAsync(bookPage);
    }

    public static async Task RemoveBookPage(int id)
    {
        await Init();
        await db.DeleteAsync<BookPage>(id);
    }

    public static async Task PurgeBookPage()
    {
        await Init();
        await db.DeleteAllAsync<BookPage>();
    }

    public static async Task<IEnumerable<BookPage>> GetBookPage()
    {
        await Init();
        var pages = await db.Table<BookPage>().ToListAsync();//Mindent adjon vissza
        return pages;
    }
}

As you can see I have also tried to give the path some other way(commented section in the code). The real problem is that I don’t really know where to start to look for the source of the problem. As the exception says there is something up with the Connection but I don’t know what I am doing wrong. I have also tried to write out the full exception using a try-catch block, but to my current knowledge I can’t write it out. And still thorws it just as if I haven’t put any try-catch block there.

When I am using the Xamarin.Essentials option it gets me the same message on the Android device and runs into an error on the Windows machine saying that it is not implemented in windows.

So to sum everything up:
I don’t know where to start looking, and at the moment the visual studio is not really helpful. ie.: that’s all I’m getting from it when I try to do anything with the database on the Android app:Image about the error in VS2022

I have already tried to give that path of the database some other way.
I have tried to clean and rebuild the project. It didn’t solved my problem.
I have also tried to completely erase the application from the one and then clean the project and rebuild/redeploy without any progress.

EDIT:
Here is the CallStack if it helps:CallStack

This is the whole thorwn exception:
[0:] System.TypeInitializationException: The type initializer for ‘SQLite.SQLiteConnection’ threw an exception.
—> System.IO.FileNotFoundException: Could not load file or assembly ‘SQLitePCLRaw.provider.dynamic_cdecl, Version=2.0.4.976, Culture=neutral, PublicKeyToken=b68184102cba0b3b’ or one of its dependencies.
File name: ‘SQLitePCLRaw.provider.dynamic_cdecl, Version=2.0.4.976, Culture=neutral, PublicKeyToken=b68184102cba0b3b’
at SQLitePCL.Batteries_V2.Init()
at SQLite.SQLiteConnection..cctor()
— End of inner exception stack trace —
at SQLite.SQLiteConnectionWithLock..ctor(SQLiteConnectionString connectionString)
at SQLite.SQLiteConnectionPool.Entry..ctor(SQLiteConnectionString connectionString)
at SQLite.SQLiteConnectionPool.GetConnectionAndTransactionLock(SQLiteConnectionString connectionString, Object& transactionLock)
at SQLite.SQLiteConnectionPool.GetConnection(SQLiteConnectionString connectionString)
at SQLite.SQLiteAsyncConnection.GetConnection()
at SQLite.SQLiteAsyncConnection.<>c__DisplayClass33_01[[SQLite.CreateTableResult, SQLite-net, Version=1.8.116.0, Culture=neutral, PublicKeyToken=null]].<WriteAsync>b__0() at System.Threading.Tasks.Task1[[SQLite.CreateTableResult, SQLite-net, Version=1.8.116.0, Culture=neutral, PublicKeyToken=null]].InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__273_0(Object obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
— End of stack trace from previous location —
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
— End of stack trace from previous location —
at MAUIDemo.Services.BookPageService.Init() in C:OnLabOnlab_HelloWorldMAUIDemoMAUIDemoServicesBookPageService.cs:line 28

And this is the InnerException:
[0:] Could not load file or assembly ‘SQLitePCLRaw.provider.dynamic_cdecl, Version=2.0.4.976, Culture=neutral, PublicKeyToken=b68184102cba0b3b’ or one of its dependencies.

2

Answers


  1. Chosen as BEST ANSWER

    I got it working. So the problem was that I installed the sql-net-pcl with the nuget manager but it seems that it wasn't enough. So I followed the exceptions that said: couldn't load .... file or dependecy. And I downloaded those dependencies manually from the Nuget manager. Oh and also added Batteries.Init() before the first init. And now it seems to be working! Thank god!


  2. i think if you just uninstall the package "sqlite-net-pcl" and reinstall it then delete the bin and obj folder then build it should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search