flutter 未捕获无效数据库异常

wljmcqd8  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(125)

要使用sqflite和sqflite_common_ffi包在Flutter中打开本地数据库,我提供了打开该数据库的路径。它在有效的数据库文件上成功。我试图捕捉异常,特别是“无效的数据库文件”。然而,什么都没有捕捉到:

try {
    final database = openDatabase(
        databaseFile!.path!,
        version: 1,
    );
} on Exception catch (e) {
    log('Error');
}

堆栈跟踪:

flutter: error SqfliteFfiException(sqlite_error: 26, , SqliteException(26): while selecting from statement, file is not a database, file is not a database (code 26)
  Causing statement: PRAGMA user_version}) DatabaseException(SqliteException(26): while selecting from statement, file is not a database, file is not a database (code 26)
  Causing statement: PRAGMA user_version) sql 'PRAGMA user_version' {details: {database: {path: C:\Users\user\Documents\GitHub\project\.gitattributes, id: 1, readOnly: false, singleInstance: true}}} during open, closing...

我尝试捕获ExceptionsDatabaseExceptions,但无法捕获错误。

lvmkulzt

lvmkulzt1#

我找到问题所在了。因为openDatabase函数是异步的,所以需要等待异常被捕获。

try {
    final database = await openDatabase(
        databaseFile!.path!,
        version: 1,
    );
} on Exception catch (e) {
    log('Error');
}

相关问题