Search code examples
c#sqliteandroid-sqlitemaui

Cannot connect to SQLLite in MAUI (.NET 8)


I have an app created in maui (.NET 7). Now I tried to update to .NET 8 and updated ALL NuGet packages. From now, I'm unable to open connection to SQLLite internal DB

    public const SQLite.SQLiteOpenFlags Flags =
        SQLite.SQLiteOpenFlags.ReadWrite |
        SQLite.SQLiteOpenFlags.Create |
        SQLite.SQLiteOpenFlags.SharedCache;

    string sPath = BCDataConstants.DatabasePath;
    // value = /data/user/0/it.gattoneroph.photoapp/files/CAPhoto.db 
    // cames from =>       Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "CAPhoto.db");


    Database = new SQLiteAsyncConnection(sPath, Flags);
    // no error but "System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception." in ALL properties

    CreateTableResult s= Database.CreateTableAsync<Tab_TabellaDatabase>().Result;
// here crashes

VS 2022 on win 11 + emulator API 30

.NET 8

NuGet Packages

  • SQLitePCLRaw.Core 2.1.8
  • SQLite 3.13.0
  • SQLlite.PCLRaw.lib.e_sqlite3 2.1.8
  • SQLitePCLRaw.bundle_e_sqlite3 2.1.8
  • SQLitePCLRaw.bundle_green 2.1.8
  • System.Data.SQLite.Core 1.0.118
  • SQLitePCLRaw.provider.dynamic_cdecl 2.1.8

What can I do?

Tried everything I know.

(EDIT)

enter image description here Connection properties after constructor

Stacktrace when I try to create table

11:07:55:509 [DOTNET] System.AggregateException: One or more errors occurred. (The type initializer for 'SQLite.SQLiteConnection' threw an exception.) 11:07:55:509 [DOTNET] ---> System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception. 11:07:55:509 [DOTNET] ---> System.TypeLoadException: VTable setup of type SQLitePCL.SQLite3Provider_e_sqlite3 failed 11:07:55:509 [DOTNET] at SQLite.SQLiteConnection..cctor() 11:07:55:509 [DOTNET] --- End of inner exception stack trace --- 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionWithLock..ctor(SQLiteConnectionString connectionString) 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionPool.Entry..ctor(SQLiteConnectionString connectionString) 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionPool.GetConnectionAndTransactionLock(SQLiteConnectionString connectionString, Object& transactionLock) 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionPool.GetConnection(SQLiteConnectionString connectionString) 11:07:55:509 [DOTNET] at SQLite.SQLiteAsyncConnection.GetConnection() 11:07:55:509 [DOTNET] at SQLite.SQLiteAsyncConnection.<>c__DisplayClass33_01[[SQLite.CreateTableResult, SQLite-net, Version=1.9.172.0, Culture=neutral, PublicKeyToken=null]].<WriteAsync>b__0() 11:07:55:509 [DOTNET] at System.Threading.Tasks.Task1[[SQLite.CreateTableResult, SQLite-net, Version=1.9.172.0, Culture=neutral, PublicKeyToken=null]].InnerInvoke() 11:07:55:509 [DOTNET] at System.Threading.Tasks.Task.<>c.<.cctor>b__281_0(Object obj) 11:07:55:509 [DOTNET] at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback> callback, Object state) 11:07:55:509 [DOTNET] --- End of stack trace from previous location --- 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionWithLock..ctor(SQLiteConnectionString connectionString) 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionPool.Entry..ctor(SQLiteConnectionString connectionString) 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionPool.GetConnectionAndTransactionLock(SQLiteConnectionString> connectionString, Object& transactionLock) 11:07:55:509 [DOTNET] at SQLite.SQLiteConnectionPool.GetConnection(SQLiteConnectionString connectionString) 11:07:55:509 [DOTNET] at SQLite.SQLiteAsyncConnection.GetConnection() 11:07:55:509 [DOTNET] at SQLite.SQLiteAsyncConnection.get_Tracer() 11:07:55:509 [DOTNET] --- End of inner exception stack trace --- 11:07:55:509 [DOTNET] at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) 11:07:55:509 [DOTNET] at System.Threading.Tasks.Task1[[SQLite.CreateTableResult, SQLite-net, Version=1.9.172.0, Culture=neutral, PublicKeyToken=null]].GetResultCore(Boolean waitCompletionNotification) 11:07:55:509 [DOTNET] at System.Threading.Tasks.Task1[[SQLite.CreateTableResult, SQLite-net, Version=1.9.172.0, Culture=neutral, PublicKeyToken=null]].get_Result() 11:07:55:509 [DOTNET] at PhotoApp.Classes.Database.BCDataConnection..ctor() in C:\Progetti


Solution

  • Found a solution... I've removed ALL sqllite NuGet packages and installed ONLY 2

    • sqlite-net-pcl v. 1.9.172
    • SQLitePCLRaw.bundle_green v2.1.8

    also changed DbName "CAPhoto.db" to "CAPhoto.db3" and dbpath to FileSystem.AppDataDirectory

    Now tables will be created successfully