3.写个帮助类,具体对某个表的操作(例如bookCatalog表)批量更新sql语句
import 'package:sqflite/sqflite.dart';
import 'package:tangerine_read/common/app_constant.dart';
import 'package:tangerine_read/local/database/read_sql.dart';
import 'package:tangerine_read/model/bookChapter.dart';
class BookCatalogHelper{
ReadSQL _sql;
static final BookCatalogHelper _instance = new BookCatalogHelper.internal();
static BookCatalogHelper get instance => _instance;
BookCatalogHelper.internal(){
_sql = ReadSQL();
}
Future close() async {
var result = _sql.close();
return result;
}
Future<bool> isBookHasCatalog(String bookId)async{
var dbClient = await _sql.db;
int count = Sqflite.firstIntValue(await dbClient.rawQuery("SELECT COUNT(*) FROM ${ReadSQL.tableCatalog} WHERE bookId=$bookId"));    return count > 0;
}
Future<int> getBookCatalogCount(String bookId)async{
var dbClient = await _sql.db;
int count = Sqflite.firstIntValue(await dbClient.rawQuery("SELECT COUNT(*) FROM ${ReadSQL.tableCatalog} WHERE bookId=$bookId"));    return count;
}
///⽬录保存sql⽐较复杂
///需要考虑该是否连载或者完结
///连载的书每次都要更新⽬录
Future<int> saveCatalogs(String bookId,List<BookChapter> catalog)async{
if(catalog == null || catalog.isEmpty){
return 0;
}
///因为考虑每⼀章的信息唯⼀,所以要给⽬录加上⼀个mixCode
catalog.forEach((bookChapter){
bookChapter.mixCode = bookId + String();
bookChapter.bookId = bookId;
});
var dbClient = await _sql.db;
bool has = await isBookHasCatalog(bookId);
if(has){
if(AppConstant.DEBUG){
print("saveCatalogs delete");
}
///delete
int deleteCount = await dbClient.rawDelete("DELETE FROM ${ReadSQL.tableCatalog} WHERE bookId=$bookId");
if(AppConstant.DEBUG){
print("saveCatalogs deleteCount:$deleteCount");
}
}
///批量往表中加数据
int startTime = w().millisecondsSinceEpoch;
var batch = dbClient.batch();
catalog.forEach((log){
batch.insert(ReadSQL.tableCatalog, Map());

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。