gpt4 book ai didi

flutter - 新创建的 SQFlite 数据库中的事务/批处理

转载 作者:行者123 更新时间:2023-12-04 13:58:02 30 4
gpt4 key购买 nike

以下仅发生在刚刚在代码中创建的数据库中。以前存在的数据库工作正常。

我有数据库助手的常用单例设置,相关部分是:

Future<Database> get database async {
// ...
db ??= await openDatabase(
path.join(await getDatabasesPath(), 'database.db'),
onCreate: (db, version) async {
final batch = db.batch();
batch.execute('CREATE TABLE table1 ...');
batch.execute('CREATE TABLE table2 ...');
await batch.commit(noResult: true);
},
// ...
return db;
}

假设数据库还不存在。我调用以下例程:
final db = await database;
await db.transaction((txn) async {
await txn.delete('table1');
final batch = txn.batch();
for (data in newData1)
batch.insert('table1', data.toJson()));
await batch.commit(noResult: true);

await txn.delete('table2');
final batch = txn.batch();
for (data in newData2)
batch.insert('table2', data.toJson()));
await batch.commit(noResult: true);
});

事务和批处理调用执行没有错误。当整个操作真正执行到最后时,停在第一个 DELETE FROM table1带有 DatabaseException 的 SQL 操作(尝试编写只读数据库(Sqlite 代码 1032)(在 Android 上运行)。

我检查了单例是单例, openDatabase没有被调用两次。我也尝试过与 exclusive: false 的交易, 没有不同。

最佳答案

问题的可能原因是无法访问该表。这种情况的已知解决方法是打开数据库并在删除之前关闭它,如本 GitHub issue thread 中所述。 .

关于flutter - 新创建的 SQFlite 数据库中的事务/批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57253131/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com