sqlite-net icon indicating copy to clipboard operation
sqlite-net copied to clipboard

.NET MAUI Project Freezes on "await db.CreateTableAsync<foo>();"

Open sethlangel opened this issue 1 year ago • 3 comments

Here is how I have my code setup

static SQLiteAsyncConnection db;

static async Task Init()
{
if (db != null)
{
    return;
}

var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyData.db");

db = new SQLiteAsyncConnection(databasePath);

await db.CreateTableAsync<GameDB>();

When I get to the line await db.CreateTableAsync<GameDB>(); my app will freeze and nothing will happen from there.

Am I missing something or is this a known issue?

Thank you in advance!

sethlangel avatar Aug 09 '23 19:08 sethlangel

I have the same issue, I tried this on iOS, which first hangs and then the app dies:

    var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyData.db");


    var db = new SQLiteAsyncConnection(databasePath);

    if (db.GetConnection().GetTableInfo("LocalSite").Count == 0)
    {
        await db.CreateTableAsync<LocalSite>();
    }

Same result without the check:

    var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyData.db");


    var db = new SQLiteAsyncConnection(databasePath);

    await db.CreateTableAsync<LocalSite>();

JanLangeDK avatar Aug 29 '23 09:08 JanLangeDK

I tried the sync method instead of async like this:

    var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyData.db");
    var db = new SQLiteConnection(databasePath);
    db.CreateTable<LocalSite>();

Which reported this back:

Message	"Exception has been thrown by the target of an invocation."	string

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception. ---> System.DllNotFoundException: e_sqlite3 at at System.Runtime.InteropServices.NativeLibrary.LoadLibraryByName(String libraryName, Assembly assembly, Nullable1 searchPath, Boolean throwOnError) at at System.Runtime.InteropServices.NativeLibrary.Load(String libraryName, Assembly assembly, Nullable1 searchPath) at at SQLitePCL.NativeLibrary.Load(String libraryName, Assembly assy, Int32 flags) at at SQLitePCL.Batteries_V2.MakeDynamic(String name, Int32 flags) at at SQLitePCL.Batteries_V2.DoDynamic_cdecl(String name, Int32 flags) at at SQLitePCL.Batteries_V2.Init() at at SQLite.SQLiteConnection..cctor() --- End of inner exception stack trace ---

JanLangeDK avatar Aug 29 '23 09:08 JanLangeDK

Found the solution here: https://stackoverflow.com/questions/46915404/the-type-initializer-for-sqlite-sqliteconnection-threw-an-exception Search for "First, using NuGet packages: If you install the sqlite-net-pcl package" Section 1 worked for me!

JanLangeDK avatar Aug 29 '23 13:08 JanLangeDK