为什么此查询在 Android Studio 的 App Inspection 的 Database Inspector 部分有效,但在 Room Query 中无效?
Why would this query work in the Database Inspector section of the App Inspection in Android Studio, but not within a Room Query?
我的本地 SQLite 数据库中有一个 table,它的 Class 如下:
@Entity(tableName = "table_bp_reading")
class BPReading(
var systolicValue: Int = 120,
var diastolicValue: Int = 80,
var pulseValue: Int = 72,
var timeStamp: String = getDateTimeStamp(),
@PrimaryKey(autoGenerate = true) var pId: Int = 0
) {
...
道的相关部分是这样的:
...
@Query("SELECT * FROM table_bp_reading WHERE timeStamp BETWEEN :startDate AND :endDate")
fun getReadingsByDateRange(startDate: String, endDate: String): Flow<List<BPReading>>
...
数据库检查器显示 table
the following entries.
运行 数据库检查器中的以下查询:
SELECT * FROM table_bp_reading WHERE timeStamp BETWEEN '2021-09-18 00:00:00' AND '2021-09-18 23:59:59'
Gives the following result.
但是,当我尝试使用以下方法观察数据时:
bpReadingViewModel.bpReadingsByDate("2021-09-18 00:00:00", "2021-09-18 23:59:59").observe(viewLifecycleOwner, {
bpReading ->
bpReading.let {
bpReadingsByDate = it
if(it.isNotEmpty()) bindDBDataToScatterChart()
}
})
我这边没有显示任何数据,查询结果为空列表。
但是请注意,如果我不使用带时间的完整时间戳,而是使用日期字符串,例如“2021-09-18”:
bpReadingViewModel.bpReadingsByDate("2021-09-18", "2021-09-18").observe(viewLifecycleOwner, {
bpReading ->
bpReading.let {
bpReadingsByDate = it
if(it.isNotEmpty()) bindDBDataToScatterChart()
}
})
而且我还将查询从之前给出的 Dao 更改为以下内容:
@Query("SELECT * FROM table_bp_reading WHERE DATE(timeStamp) BETWEEN :startDate AND :endDate")
fun getReadingsByDateRange(startDate: String, endDate: String): Flow<List<BPReading>>
它有效,并且它 returns 当天的 3 个条目是它的本意。但是,一旦我更改上面的日期范围,以在 2021-09-18 和 2021-09-19 之间进行过滤,它就会再次停止工作。
请帮忙,我对这里发生的事情感到很困惑。
编辑:
这是来自 Repository 的相关调用:
fun readingsByDateRange(s: String, e: String): Flow<List<BPReading>> =
bpReadingDao.getReadingsByDateRange(e, s)
以及来自 ViewModel 的相关调用 class:
fun bpReadingsByDate(s: String, e: String): LiveData<List<BPReading>> =
repository.readingsByDateRange(s, e).asLiveData()
这就是为什么它确实是 bpReadingsByDate,而不是 getReadingsByDateRane。抱歉,这是我的错误,因为与跨文件命名函数不一致。
查询没有问题(已测试),从您的评论和编辑的问题来看,您交换了开始和结束时间戳,这通常会导致没有任何选择。
所以你应该把bpReadingDao.getReadingsByDateRange(e, s)
改成bpReadingDao.getReadingsByDateRange(s, e)
。
我的本地 SQLite 数据库中有一个 table,它的 Class 如下:
@Entity(tableName = "table_bp_reading")
class BPReading(
var systolicValue: Int = 120,
var diastolicValue: Int = 80,
var pulseValue: Int = 72,
var timeStamp: String = getDateTimeStamp(),
@PrimaryKey(autoGenerate = true) var pId: Int = 0
) {
...
道的相关部分是这样的:
...
@Query("SELECT * FROM table_bp_reading WHERE timeStamp BETWEEN :startDate AND :endDate")
fun getReadingsByDateRange(startDate: String, endDate: String): Flow<List<BPReading>>
...
数据库检查器显示 table the following entries.
运行 数据库检查器中的以下查询:
SELECT * FROM table_bp_reading WHERE timeStamp BETWEEN '2021-09-18 00:00:00' AND '2021-09-18 23:59:59'
Gives the following result.
但是,当我尝试使用以下方法观察数据时:
bpReadingViewModel.bpReadingsByDate("2021-09-18 00:00:00", "2021-09-18 23:59:59").observe(viewLifecycleOwner, {
bpReading ->
bpReading.let {
bpReadingsByDate = it
if(it.isNotEmpty()) bindDBDataToScatterChart()
}
})
我这边没有显示任何数据,查询结果为空列表。
但是请注意,如果我不使用带时间的完整时间戳,而是使用日期字符串,例如“2021-09-18”:
bpReadingViewModel.bpReadingsByDate("2021-09-18", "2021-09-18").observe(viewLifecycleOwner, {
bpReading ->
bpReading.let {
bpReadingsByDate = it
if(it.isNotEmpty()) bindDBDataToScatterChart()
}
})
而且我还将查询从之前给出的 Dao 更改为以下内容:
@Query("SELECT * FROM table_bp_reading WHERE DATE(timeStamp) BETWEEN :startDate AND :endDate")
fun getReadingsByDateRange(startDate: String, endDate: String): Flow<List<BPReading>>
它有效,并且它 returns 当天的 3 个条目是它的本意。但是,一旦我更改上面的日期范围,以在 2021-09-18 和 2021-09-19 之间进行过滤,它就会再次停止工作。
请帮忙,我对这里发生的事情感到很困惑。
编辑: 这是来自 Repository 的相关调用:
fun readingsByDateRange(s: String, e: String): Flow<List<BPReading>> =
bpReadingDao.getReadingsByDateRange(e, s)
以及来自 ViewModel 的相关调用 class:
fun bpReadingsByDate(s: String, e: String): LiveData<List<BPReading>> =
repository.readingsByDateRange(s, e).asLiveData()
这就是为什么它确实是 bpReadingsByDate,而不是 getReadingsByDateRane。抱歉,这是我的错误,因为与跨文件命名函数不一致。
查询没有问题(已测试),从您的评论和编辑的问题来看,您交换了开始和结束时间戳,这通常会导致没有任何选择。
所以你应该把bpReadingDao.getReadingsByDateRange(e, s)
改成bpReadingDao.getReadingsByDateRange(s, e)
。