如何使用 whereArgs select SQLite Flutter 中的 DISTINCT 条目?
How to select DISTINCT entries in SQLite Flutter with whereArgs?
我想 return 来自 SQLITE 中 table 的不同记录,通过给出一些条件,例如 return 所有不同的记录,其中 ID = x(其中 x 将被采用由用户)。
我怎样才能做到这一点?
使用 SQFlite package 您可以使用 query
或 rawQuery
来执行此操作,在这两种情况下您都需要将 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],
);
//...
}
我想 return 来自 SQLITE 中 table 的不同记录,通过给出一些条件,例如 return 所有不同的记录,其中 ID = x(其中 x 将被采用由用户)。
我怎样才能做到这一点?
使用 SQFlite package 您可以使用 query
或 rawQuery
来执行此操作,在这两种情况下您都需要将 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],
);
//...
}