sqlite 我无法一次将超过50个数据保存到数据库,

hgtggwj0  于 2022-12-23  发布在  SQLite
关注(0)|答案(1)|浏览(148)

我想要的:当应用程序第一次打开时,我将从服务接收到的数据添加到列表中,我想用for循环将添加到列表中的数据保存到我的sqflite数据库中。
问题:数据(266个单位)从服务添加到列表中,没有任何问题。前50个数据添加到数据库中,没有任何问题。50个数据之后,应用程序不执行任何操作。
我的代码之前运行良好,我不明白发生了什么,所以它开始给出错误。
我在闪屏上调用的方法:

_getData() async {
    await Provider.of<FirestoreDataNotifier>(context, listen: false)
        .fetchRecommendedContentsOfTheWeek();

    Config.sharedPreferences = await SharedPreferences.getInstance();
  }

通知程序方法:

Future<void> fetchRecommendedContentsOfTheWeek() async {
    List<RecommendedVideoModel> _tempList = [];
    _isCompletely = false;
    try {
      Some transactions...

      await getAndAddData();

      notifyListeners();
    } catch (e) {
      e.toString();
    }
  }

print('-〉获取和添加数据成功!');它不会继续这条线,而是停留在循环中。

Future<void> getAndAddData() async {
    List<ContentModel> _list = [];
    _list = await FirestoreService().getAllData();

    for (var element in _list) {
      await AllContentDatabaseHelper.instance.add(
        ContentModel(
            title: element.title,
            thumbnailUrl: element.thumbnailUrl,
            content: element.content,
            id: element.id,
            categoryName: element.categoryName,
            categoryId: element.categoryId,
            estimatedTime: element.estimatedTime,
            publishedDate: element.publishedDate,
            videoUrl: element.videoUrl!.isEmpty ? ' ' : element.videoUrl),
      );
    }
    print('-> getAndAddData Successfully!');
    _list.clear();
    await getDataSize();
    _isCompletely = true;
    notifyListeners();
  }

和添加方法:

Future<void> add(ContentModel contentModel) async {
    Database db = await instance.database;
    await db.insert(_tableName, contentModel.toMap());
    print('-> OK!');
  }
ljo96ir5

ljo96ir51#

试着用一批,

batch = db.batch();
batch.insert(_tableName, contentModel.toMap());
results = await batch.commit(noResult: true);

由此:https://pub.dev/packages/sqflite

    • 更新日期:**

既然第一个答案不起作用,那就试试原始方法,比如:

insertData(List< ContentModel> rows) async {
    final db = await database;
    var buffer = new StringBuffer();
    rows.forEach((c) {
      if (buffer.isNotEmpty) {
        buffer.write(",\n");
      }
      buffer.write("(");
      buffer.write(c.id);
      buffer.write(", '");
      buffer.write(c.title);
      buffer.write("', '");
      buffer.write(c.thumbnailUrl);
      buffer.write("', '");
      buffer.write(c.content);
      buffer.write("', '");
      buffer.write(c.categoryName);
      buffer.write("', ");
      buffer.write(c.categoryId);
      buffer.write(", '");
      buffer.write(c.estimatedTime);
      buffer.write("', '");
      buffer.write(c.publishedDate);
      buffer.write("', '");
      buffer.write(c.videoUrl);
      buffer.write("'),");
    });
    var raw =
        await db.rawInsert("INSERT Into $_tableName (id,title, thumbnailUrl, content, categoryName, categoryId, estimatedTime, publishedDate,videoUrl)"
            " VALUES ${buffer.toString()}");
    return raw;
  }

为了得到这样的东西:

INSERT INTO 'tablename' ('column1', 'column2', 'column3') VALUES
                ('data1', 'data2', 'data3'),
                ('data1', 'data2', 'data3'),
                ('data1', 'data2', 'data3');

相关问题