如何在 DAO 查询中使用 DISTINCT 过滤 Room 数据库中的重复字段(在列中)
how to filter duplicate fields (in column) in Room database using DISTINCT in DAO Query
我想做的是从一列中检索数据列表,同时忽略重复项。我不想删除重复项,因为它们表示每个练习都有一组新的。我尝试使用 DISTINCT
但我遇到了一个错误:
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
我的房间 table 看起来是这样的:
workout_table
id Date Category Exercise weight
---|-----|------------|-------------|---------
1 |Mon | chest | Bench press | 399
2 |Mon | chest | Bench press | 244
5 |Mon | chest | Cross over | 555
3 |Mon | chest | Cross over | 300
4 |Tue | Triceps | Skull Crush | 200
6 |Tue | Shoulders | lat raise | 500
7 |Tue | Shoulders | front raise | 500
它是这样描述的:
@Entity (tableName = "workout_table")
class WorkoutTable(
@PrimaryKey (autoGenerate = true)
val id: Int,
@ColumnInfo(name = "Date")
val date: String,
@ColumnInfo(name = "Category")
val cat: String?,
@ColumnInfo(name = "Exercise")
val exercise: String?,
@ColumnInfo(name = "Weight")
val weight: Double
)
我试过这样查询:(DAO)
@Query ("SELECT DISTINCT Exercise FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<WorkoutTable>>
我希望收到:(假设收到日期是 Mon
)
- 卧推
- 交叉
如果收到的日期是 Tue
:
- 碎颅
- 纬度提升
- 前平举
基本上,我每天只需要每个练习中的一个,尽管一天可能会有重复。
存储库:
val getOneExerciseToday : LiveData<List<WorkoutTable>> = wordDao.getOneExerciseToday(todayIS())
视图模型:
val oneExerciseToday: LiveData<List<WorkoutTable>>
init: oneExerciseToday = repository.getOneExerciseToday
如果我删除不同的尝试并像这样放置它:
@Query ("SELECT * FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<WorkoutTable>>
一切正常,但当然它只打印一天的所有内容和副本。
我还尝试检查代码以了解实际的错误描述,但结果并未给出任何错误。而且代码在任何地方都没有显示错误。我也试过重建项目,清理代码并使缓存无效并重新启动
如果您需要唯一不同的字段,您可以从您的方法中获取字符串列表(只是练习的名称):
@Query ("SELECT DISTINCT Exercise FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<String>> // <- List<WorkoutTable> replaced by List<String>
如果这是你想要的,你应该在你的 Repository 和 ViewModel 中使用 List。
我想做的是从一列中检索数据列表,同时忽略重复项。我不想删除重复项,因为它们表示每个练习都有一组新的。我尝试使用 DISTINCT
但我遇到了一个错误:
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
我的房间 table 看起来是这样的:
workout_table
id Date Category Exercise weight
---|-----|------------|-------------|---------
1 |Mon | chest | Bench press | 399
2 |Mon | chest | Bench press | 244
5 |Mon | chest | Cross over | 555
3 |Mon | chest | Cross over | 300
4 |Tue | Triceps | Skull Crush | 200
6 |Tue | Shoulders | lat raise | 500
7 |Tue | Shoulders | front raise | 500
它是这样描述的:
@Entity (tableName = "workout_table")
class WorkoutTable(
@PrimaryKey (autoGenerate = true)
val id: Int,
@ColumnInfo(name = "Date")
val date: String,
@ColumnInfo(name = "Category")
val cat: String?,
@ColumnInfo(name = "Exercise")
val exercise: String?,
@ColumnInfo(name = "Weight")
val weight: Double
)
我试过这样查询:(DAO)
@Query ("SELECT DISTINCT Exercise FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<WorkoutTable>>
我希望收到:(假设收到日期是 Mon
)
- 卧推
- 交叉
如果收到的日期是 Tue
:
- 碎颅
- 纬度提升
- 前平举
基本上,我每天只需要每个练习中的一个,尽管一天可能会有重复。
存储库:
val getOneExerciseToday : LiveData<List<WorkoutTable>> = wordDao.getOneExerciseToday(todayIS())
视图模型:
val oneExerciseToday: LiveData<List<WorkoutTable>>
init: oneExerciseToday = repository.getOneExerciseToday
如果我删除不同的尝试并像这样放置它:
@Query ("SELECT * FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<WorkoutTable>>
一切正常,但当然它只打印一天的所有内容和副本。
我还尝试检查代码以了解实际的错误描述,但结果并未给出任何错误。而且代码在任何地方都没有显示错误。我也试过重建项目,清理代码并使缓存无效并重新启动
如果您需要唯一不同的字段,您可以从您的方法中获取字符串列表(只是练习的名称):
@Query ("SELECT DISTINCT Exercise FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<String>> // <- List<WorkoutTable> replaced by List<String>
如果这是你想要的,你应该在你的 Repository 和 ViewModel 中使用 List。