【问题标题】:Transaction/batch in a newly created SQFlite database新创建的 SQFlite 数据库中的事务/批处理
【发布时间】:2019-12-06 18:02:56
【问题描述】:

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

我为数据库助手设置了通常的单例设置,相关部分是:

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 SQL 操作上停止,并带有 DatabaseException(尝试写入只读数据库(Sqlite 代码 1032) (在 Android 上运行)。

我检查了单例单例,openDatabase 没有被调用两次。我也试过exclusive: false的交易,没区别。

【问题讨论】:

  • 您试图删除数据库中尚不存在的表。

标签: flutter sqflite


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2010-12-02
    • 1970-01-01
    • 2015-01-18
    相关资源
    最近更新 更多