sqflite batch.commit() 不与 Flutter 一起工作



我正在尝试进行批量更新并在颤振中使用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 = '++'");
}

嗨,我认为您在功能中批处理时遇到问题。第一批在executeBatch声明,第二批在buildBatch声明,因此这是两个不同的对象,并且第一个没有可执行语句。我只是有同样的问题,我找到了你的问题。在我的解决方案中,创建了一个对象布奇,我只是调用_database.batch().execuste(sql);什么是不正确的。内德:

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

最新更新