要使用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...
我尝试捕获Exceptions
和DatabaseExceptions
,但无法捕获错误。
1条答案
按热度按时间lvmkulzt1#
我找到问题所在了。因为openDatabase函数是异步的,所以需要等待异常被捕获。