【问题标题】:In the Flutter Isar database, How to check database schema is empty or not?在 Flutter Isar 数据库中,如何检查数据库模式是否为空?
【发布时间】:2022-11-12 18:59:48
【问题描述】:

在 ObjectBox 数据库中,我们可以使用 box.isEmpty() 方法检查框是否为空。同样如何检查 Isar 数据库模式是否为空。因为在我的应用程序中我有一个场景,只有在数据库为空时才将数据插入数据库。

【问题讨论】:

  • 请提供足够的代码,以便其他人可以更好地理解或重现该问题。

标签: database flutter dart is-empty isar


【解决方案1】:

可能有更好的方法来做到这一点,但这有效(这将加载所有条目,所以如果它很大,那么这种方法不是很有效):

db = await Isar.open([DrawSchema], directory: dbPath);

{Your stuff happens here}

final isEmpty = (await db.draws.where().findAll()).length==0;

YMMV

【讨论】:

    【解决方案2】:

    假设您有一个名为Client 的模式。您可以通过获取架构的计数来检查 Client 集合是否为空:

    final isar = await Isar.open([ClientSchema]);
    final count = await isar.clients.count();
    

    从文档中:

    返回此集合中的对象总数。对于非网络 应用程序,这种方法速度极快,并且与应用程序的数量无关 集合中的对象。

    【讨论】:

      【解决方案3】:

      你可以通过使用构建查询

      例如

      Future<List<DonorModel>> loadDonors(int limit) async {
          final isar = ref.read(isarDatabaseProvider);
          final donors =
              await isar.donorModels.buildQuery<DonorModel>(limit: limit).findAll();
          return donors;
        }
      

      我不知道它不能直接使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-07
        • 2016-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多