52ll.org
52ll.org

visual studio 2022 maui sqlite-net-pcl 解决报错

参见上文,终于在控制台里看到了具体的报错

The type initializer for 'SQLite.SQLiteConnection' threw an exception.
   at BookManage.Pages.Book.Index.LoadBooks()
   at BookManage.Pages.Book.Index.OnInitialized()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at BookManage.Pages.Book.Index.LoadBooks()
   at BookManage.Pages.Book.Index.OnInitialized()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at BookManage.Pages.Book.Create.AddBook(Book book)
   at BookManage.Pages.Book.Create.b__0_10()
   at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[Object](MulticastDelegate delegate, Object arg)
   at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync(Object arg)
   at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)
   at Microsoft.AspNetCore.Components.EventCallback`1[[Microsoft.AspNetCore.Components.Web.MouseEventArgs, Microsoft.AspNetCore.Components.Web, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].InvokeAsync(MouseEventArgs arg)
   at BlazorComponent.BButton.HandleOnClick(MouseEventArgs args)
   at Masa.Blazor.MButton.HandleOnClick(MouseEventArgs args)
   at BlazorComponent.BComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at BlazorComponent.BComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

之后有尝试编辑 你的项目\Platforms\Android\AndroidManifest.xml ,添加了这些权限(我不是很确定添加这些与解决上面错误的有没有关系)

	<uses-permission android:name="android.permission.FLASHLIGHT" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
https://52ll.org/wp-content/uploads/2023/03/Snipaste_2023-03-26_11-08-32.png

权限说明

这段代码是 Android 应用程序的权限声明,它是在应用程序清单文件(AndroidManifest.xml)中声明的。这些声明告诉操作系统你的应用需要哪些权限才能正确运行。

- `android.permission.ACCESS_NETWORK_STATE`: 允许应用程序访问网络连接状态信息。
- `android.permission.FLASHLIGHT`: 允许应用程序控制闪光灯或手电筒。
- `android.permission.INTERNET`: 允许应用程序打开网络套接字,进行网络通信。
- `android.permission.READ_EXTERNAL_STORAGE`: 允许应用程序读取外部存储器(例如SD卡)上的文件。
- `android.permission.WRITE_EXTERNAL_STORAGE`: 允许应用程序写入外部存储器(例如SD卡)上的文件。
- `android.permission.ACCESS_COARSE_LOCATION`: 允许应用程序获取大概的位置信息(使用基站和WiFi等)。
- `android.permission.ACCESS_FINE_LOCATION`: 允许应用程序获取精确的位置信息(使用GPS等)。
- `android.permission.READ_CONTACTS`: 允许应用程序读取用户的联系人信息。
- `android.permission.WRITE_CONTACTS`: 允许应用程序修改或删除用户的联系人信息。

之后又遇到

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.'

解决方法是安装这个nuget包

SQLitePCLRaw.provider.dynamic_cdecl

我还有安装这个nuget包,如果你安装了上面的包还是不行的话可以试试看

SQLitePCL.raw
https://52ll.org/wp-content/uploads/2023/03/Snipaste_2023-03-26_11-12-15.png

然后遇到

Could not open database file: book.db (CannotOpen)
   at SQLite.SQLiteConnection..ctor(SQLiteConnectionString connectionString)
   at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
   at BookManage.Pages.Book.Index.LoadBooks()
   at BookManage.Pages.Book.Index.OnInitialized()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

这里找到了一个讨论,我之前的代码是这样的,在 Windows 上可以跑,在 Android 上跑就遇到了错误

using (var connection = new SQLiteConnection("book.db"))

之后找到了这个讨论,看起来是数据库文件存放路径的问题

https://stackoverflow.com/questions/56614778/could-not-open-database-file-misuse-sqlliteasyncconnection

一个回答的建议是修改代码

Path.Combine(ApplicationData.Current.LocalFolder.Path, FILE_NAME);

粘贴到 visual studio 中提示过时了,问了 openai 说可以改成

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

我的完整代码

    protected override void OnInitialized()
    {
        LoadBooks(); // 调用 LoadBooks 函数加载书籍列表
    }

    private void LoadBooks()
    {
        string db_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "book.db");
        using (var connection = new SQLiteConnection(db_path, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite))
        {
            if (!connection.GetTableInfo("book").Any())
            {
                connection.CreateTable();
            }
            var books = (from b in connection.Table() select b).ToList();
            _Books = books;
        }
    }

最后终于可以正常跑起来了

发表回复

textsms
account_circle
email

52ll.org

visual studio 2022 maui sqlite-net-pcl 解决报错
参见上文,终于在控制台里看到了具体的报错 The type initializer for 'SQLite.SQLiteConnection' threw an exception. at BookManage.Pages.Book.Index.LoadBooks() at BookMan…
扫描二维码继续阅读
2023-03-26