【问题标题】:sqflite batch.commit() not working with fluttersqflite batch.commit() 不适用于颤振
【发布时间】:2020-06-07 06:43:26
【问题描述】:

我正在尝试在颤振中使用 sqflite 进行批量更新和插入,但提交似乎不起作用。由于没有错误或异常,我不知道发生了什么。以下是我传递列表并基于列表 id 在数据库中进行更新或插入的两种方法。

Future<void> executeBatch(List<MusicData> _list, int version) async {
await openDb();
batch = _database.batch();
try {
  for (var i = 0; i < _list.length; i++) {
    buildBatch(_list[i]);
  }
  batch.commit(noResult: true);
  // Future<List> result =  batch.commit();          
  SharedPreferences prefs = await SharedPreferences.getInstance();
  await prefs.setInt('dbversion', version);

} catch (e) {
  print(e);
}
}  

Future<Batch> buildBatch(MusicData musicData) async {
    await openDb();
    int id = musicData.id;
    Future<List<MusicData>> list1 =
        getSongList("select * from songs where id=$id");
    List<MusicData> list = await list1;    
    if (list.length != 0) {
      batch.rawUpdate(
          "UPDATE SONGS SET pdfpage = ?, linkid = ?, title = ?, album = ?, songURL = ?, hindiName = ?, mname = ?, msign = ?, other1 = ?, other2 = ?, ename = ?, esign = ?, language = ?,songtext = ? WHERE id = ?",
          [
            musicData.id,
            musicData.pdfpage,
            musicData.linkid,
            musicData.title,
            musicData.album,
            musicData.songURL,
            musicData.hindiName,
            musicData.mname,
            musicData.msign,
            musicData.other1,
            musicData.other2,
            musicData.ename,
            musicData.esign,
            musicData.language,
            musicData.songtext
          ]);
      print("Record updated in db $id");
      // _database.close();
    } else {
      batch.insert('SONGS', musicData.toMap());
      print("Record inserted in db $id");
    }
    // List<Map> map = await _database.rawQuery("select * from SONGS where id = '++'");
  }

【问题讨论】:

  • 我查看了数据库文件,没有看到任何记录被添加。我正在根据记录是否存在进行更新和插入。
  • 是的..确实如此..我也尝试捕获结果并查看有效数据。
  • 是的.. 它的安卓设备。我正在使用 DB Browser for SQLLite 来查看记录。当用户安装应用程序时,它会创建记录,我可以看到这些记录,然后当用户重新启动时,我尝试进行更新和插入,但我没有看到任何这些操作发生。
  • 不,我可以为 200 行执行此操作吗?我尝试将其作为 Map 启动,然后将其转换为字符串,但批处理不接受。
  • 正如您在代码中看到的,我正在使用 for 循环并构建批量更新/插入。

标签: sqlite flutter sqflite


【解决方案1】:

您好,我认为您的函数中的批处理有问题。第一批声明在executeBatch,第二批声明在buildBatch,所以这是两个不同的对象,第一个没有可执行语句。我只是有同样的问题,我找到了你的问题。在我的解决方案中创建了一个对象,我只是调用了_database.batch().execuste(sql);,这是不正确的。内德:

batch = _database.batch(); 
batch.execute(sql);

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 2020-02-26
    • 2021-08-07
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2021-10-06
    • 2021-08-15
    • 2019-04-17
    相关资源
    最近更新 更多