Skip to content

[Bug] On iOS: The type initializer for 'SQLite.SQLiteConnection' threw an exception (Android works) #3211

Closed

Description

@jallmen

Description

  public class Database
  {
      public Database()
      {
          var dbPath = Path.Combine(FileSystem.AppDataDirectory, "databaseTest.db3");

          Connection = new SQLiteAsyncConnection(dbPath);
          Connection.CreateTableAsync<User>().Wait();
      }

      public SQLiteAsyncConnection Connection { get; set; }
  }

This seems to work fine with Android applications allowing me to manipulate the database from anywhere.

However, when I run this application through an IOS simulator with an active Mac connection I get the following error:

"The type initializer for 'SQLite.SQLiteConnection' threw an exception."

I believe that an active connection to an SQLite database cannot be resolved but I don't know why.

Steps to Reproduce

  1. Create a database class that uses SQLite-net-plc to establish a connection with a database in the constructor
  2. Pass this class through the MAUI dependency injection

Expected Behavior

Database connection connects and you can use the database

Actual Behavior

"The type initializer for 'SQLite.SQLiteConnection' threw an exception."

Basic Information

  • Version with issue: 6.0.101-preview.9.1843
  • Last known good version: n/a
  • IDE: VS 2022
  • Platform Target Frameworks:
    • iOS: 15.0
    • Android: n/a
    • UWP: n/a
  • Android Support Library Version: n/a
  • Nuget Packages: sqlite-net-plc, SQLitePCLRaw.provider.dynamic_cdecl
  • Affected Devices: Iphone 11 Pro Max IOS 15.0

Screenshots

n/a

Reproduction Link

not available

Workaround

Activity

  1. changed the title [-][Bug] [/-] [+][Bug] On iOS: The type initializer for 'SQLite.SQLiteConnection' threw an exception (Android works)[/+] on Nov 1, 2021
  2. VladislavAntonyuk commented on Nov 5, 2021

    @VladislavAntonyuk
    Contributor

    I don't think the issue is with the MAUI. by the way I created a test project where SQLite works with Maui https://github.com/VladislavAntonyuk/KanbanBoard

  3. jsuarezruiz commented on Nov 5, 2021

    @jsuarezruiz
    Contributor

    Could you attach a sample where we can reproduce the issue?

  4. jallmen commented on Nov 5, 2021

    @jallmen
    Author

    I don't think the issue is with the MAUI. by the way I created a test project where SQLite works with Maui https://github.com/VladislavAntonyuk/KanbanBoard

    Does this work on IOS?

  5. VladislavAntonyuk commented on Nov 5, 2021

    @VladislavAntonyuk
    Contributor

    yes,
    iOS
    image

    and macOS
    image

    make sure you set the provider (SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());) before MauiApp initialization

  6. jallmen commented on Nov 9, 2021

    @jallmen
    Author

    yes, iOS image

    and macOS image

    make sure you set the provider (SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());) before MauiApp initialization

    Do you mean before this line:
    image

  7. jallmen commented on Nov 10, 2021

    @jallmen
    Author

    Yes that fixed the issue, Thanks.

  8. mattleibow commented on Nov 12, 2021

    @mattleibow
    Member

    Awesome!

  9. wuyuansushen commented on Nov 30, 2021

    @wuyuansushen

    Thanks a lot.
    The List of nuget packages should be used in preview:

    • CommunityToolkit.Maui
    • CommunityToolkit.Mvvm
    • sqlite-net-pcl
    • SQLiteNetExtensions.Async
    • SQLitePCLRaw.core
    • SQLitePCLRaw.bundle_green
    • SQLitePCLRaw.provider.dynamic_cdecl
    • Xamarin.CommunityToolkit.MauiCompat

    Some of these nuget packages have the Preview version only, so search them with Include prelease on in Nuget browser.

  10. RaspeR87 commented on Feb 2, 2022

    @RaspeR87

    @VladislavAntonyuk your sample on Github didn't work anymore on latest Visual Studio 17.1.0 Preview 5 & MAUI Preview 12.
    It doesn't recognise SQLite3Provider_sqlite3 in the AppDelegate class. I can get it to build if I change it to SQLite3Provider_e_sqlite3, but then the app crashes on start-up with the following:

    [0:] An error occurred: 'e_sqlite3'. Callstack: ' at SQLitePCL.SQLite3Provider_e_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number()
    at SQLitePCL.raw.SetProvider(ISQLite3Provider imp)

    As you mentioned on your blog, it can be builded just with CLI command dotnet build -f net6.0-ios and not with UI.
    But then app cannot be pushed to iOS simulator with command dotnet build -t:Run -f net6.0-ios -p:_DeviceName=:v2:udid=<UDID>

    It can be builded with UI too only if you add one additional Nuget package: SQLitePCLRaw.provider.sqlite3
    But when you want to run that app on iOS simulator you get this error:
    2022-02-02_20-06-06

    @hartez I think this bug is not Done. Is there any way how we can currently use SQL database in our MAUI app (in iOS and Android app)?

  11. VladislavAntonyuk commented on Feb 2, 2022

    @VladislavAntonyuk
    Contributor

    @VladislavAntonyuk your sample on Github didn't work anymore on latest Visual Studio 17.1.0 Preview 5 & MAUI Preview 12.

    It doesn't recognise SQLite3Provider_sqlite3 in the AppDelegate class. I can get it to build if I change it to SQLite3Provider_e_sqlite3, but then the app crashes on start-up with the following:

    [0:] An error occurred: 'e_sqlite3'. Callstack: ' at SQLitePCL.SQLite3Provider_e_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number()

    at SQLitePCL.raw.SetProvider(ISQLite3Provider imp)

    As you mentioned on your blog, it can be builded just with CLI command dotnet build -f net6.0-ios and not with UI.

    But then app cannot be pushed to iOS simulator with command dotnet build -t:Run -f net6.0-ios -p:_DeviceName=:v2:udid=<UDID>

    It can be builded with UI too only if you add one additional Nuget package: SQLitePCLRaw.provider.sqlite3

    But when you want to run that app on iOS simulator you get this error:

    2022-02-02_20-06-06

    @hartez I think this bug is not Done. Is there any way how we can currently use SQL database in our MAUI app (in iOS and Android app)?

    I haven't installed preview 12 on my macOS (it requires .net 6.0.2 which hasn't released yet). I will take a look and update the app.
    The app should work on Android

  12. RaspeR87 commented on Feb 2, 2022

    @RaspeR87

    @VladislavAntonyuk no, I am talking about VS and MAUI on Windows and remote debugging on remote iOS simulator via Mac

  13. VladislavAntonyuk commented on Feb 2, 2022

    @VladislavAntonyuk
    Contributor

    @VladislavAntonyuk no, I am talking about VS and MAUI on Windows and remote debugging on remote iOS simulator via Mac

    By the way what is your iOS version?

  14. RaspeR87 commented on Feb 2, 2022

    @RaspeR87

    @VladislavAntonyuk no, I am talking about VS and MAUI on Windows and remote debugging on remote iOS simulator via Mac

    By the way what is your iOS version?

    iOS 15.2

  15. VladislavAntonyuk commented on Feb 11, 2022

    @VladislavAntonyuk
    Contributor

    It is fixed. I update the sample app. Please try again

  16. ghost locked as resolved and limited conversation to collaborators on Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform/ioss/needs-infoIssue needs more info from the authort/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions