Sqlite.SqliteException(0x80004005):SQLite错误14:“无法打开数据库文件”

问题描述 投票:0回答:1

我已经两天无法解决这个问题了。我正在开发我的第一个 MAUI Hybrid-Blazor 应用程序,它具有本地 SQLite 数据库。

我收到以下错误消息:

“[*] - SQLite错误14:'无法打开数据库文件'。-位于Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc,sqlite3 db) 在 Microsoft.Data.Sqlite.SqliteConnectionInternal..ctor(SqliteConnectionStringBuilder 连接选项,SqliteConnectionPool 池) 在 Microsoft.Data.Sqlite.SqliteConnectionPool.GetConnection() 在 Microsoft.Data.Sqlite.SqliteConnectionFactory.GetConnection(SqliteConnection 外连接) 在 Microsoft.Data.Sqlite.SqliteConnection.Open() 在 System.Data.Common.DbConnection.OpenAsync(CancellationToken CancellationToken) --- 先前位置的堆栈跟踪结束 --- 在 C:\Users\User\Desktop\TheBoss\TheBoss\TheBoss\Services\ServiceData.cs 中的 TheBoss.Services.ServiceData.GetAllCryptocurrenciesAsync():第 26 行 - [4/7/2024 9:33:57PM]"

我制作了单独的控制台应用程序,只是为了确保我没有对查询或数据库做错什么,但控制台应用程序中的一切都按假设工作。我可以连接到数据库并执行查询。

我还缺少任何其他设置吗?如有任何帮助,我们将不胜感激。

服务数据.cs

public class ServiceData
{
    private readonly string _connectionString;
    public ServiceData(string connectionString)
    {
        _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
    }

    public async Task<List<Cryptocurrency>?> GetAllCryptocurrenciesAsync()
    {
        try
        {
            var items = new List<Cryptocurrency>();

            using (var connection = new SqliteConnection($"Data Source={_connectionString}"))
            {
                await connection.OpenAsync();

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT * FROM cryptocurrencies;";

                    using (var reader = await command.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            var item = new Cryptocurrency
                            {
                                Id = reader.GetInt32(0),
                                Name = reader.GetString(1),
                                Icon = reader.GetString(2)
                            };
                            items.Add(item);
                        }
                    }
                }
            }

            return items;
        }
        catch (Exception ex)
        {
            // Append the exception details to the exceptions file
            string exceptionmsg = $"[*] - {ex.Message} - {ex.StackTrace} - [{DateTime.Now}]";

            return null;
        }
    }

MauiProgram.cs

    public static class MauiProgram
    {
        private static readonly string _databasePath = Path.Combine(FileSystem.AppDataDirectory, "Data/theboss.db");
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });

            // Add SQLite connection
            var connectionStringBuilder = new SqliteConnectionStringBuilder
            {
                DataSource = $"Data Source= " + _databasePath
            };
            var connection = new SqliteConnection(connectionStringBuilder.ConnectionString);
            builder.Services.AddSingleton(connection);


            builder.Services.AddMauiBlazorWebView();

#if DEBUG
            builder.Services.AddBlazorWebViewDeveloperTools();
            builder.Logging.AddDebug();
#endif

            return builder.Build();
        }
    }

enter image description here enter image description here

我做了一些故障排除,但仍然无法解决它:

string databasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "theboss.db");
"/data/user/0/com.theboss.myapp/files/Data/theboss.db"
Exception: Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 14: 'unable to open database file'.
 

string databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "theboss.db3");
"/data/user/0/com.theboss.myapp/files/theboss.db3"
Exception: {Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: cryptocurrencies'.

 
maui maui-blazor
1个回答
0
投票

从 Xamarin 迁移到 MAUI 时,我必须更改 .db 文件的路径。在 Xamarin 中,我有

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
。在毛伊岛我不得不改为:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

© www.soinside.com 2019 - 2024. All rights reserved.