如何使用whereArgs在SQLite Flutter中选择DISTINCT条目?

问题描述 投票:0回答:1

[我想通过提供一些条件,例如在ID = x的情况下返回所有不同的记录,来返回SQLITE flutter中的表的不同记录,其中ID = x(其中x将由用户使用)。

我该如何实现?

sqlite flutter distinct
1个回答
0
投票

使用SQFlite package,您可以使用queryrawQuery来执行此操作,在两种情况下,您都需要将where arguments作为array传递,对于distinct operator,您可以它如下:

  /// [GET] Get all the data by date using a query
  getData(String date) async {
    final db = await database;
    final response = await db.query(
      "TableExample",
      distinct: true,
      orderBy: 'id',
      where: 'date = ?',
      whereArgs: [date],
    );
    //...
  }

  /// [GET] Get all the data by date using a rawQuery
  getDailyPollResponsesPerRangeDate(String date) async {
    final db = await database;
    final response = await db.rawQuery(
      "SELECT DISTINCT TableExample.id, TableExample.date "
      "FROM TableExample "
      "WHERE TableExample.date == ? "
      "ORDER BY TableExample.date DESC",
      [date],
    );
    //...
  }
© www.soinside.com 2019 - 2024. All rights reserved.