我检查了我的房间数据库,从字符串日期到日期,它被存储为一个使用类型转换器的Long类型。我无法过滤特定时间的结果,例如-7天或3个月等。以下是我的配置:
查询
SELECT *
FROM moodBeforetable
WHERE moodBeforetable.workoutDate >= datetime('now', '-1 year')
ORDER BY moodBeforetable.workoutDate DESC
LiveData<List<WorkoutLogsAllPojo>> getAllMoodLogs();当WHERE子句被注释掉时,查询可以正常工作,但是,如果我在上面包含该行,则不会返回任何数据。
此moodBeforetable.workoutDate变量是一个日期,但它在数据库中存储为Long :1590705660000
类型转换器
@TypeConverter
public static Date toDate(Long timestamp){
return timestamp == null ? null : new Date(timestamp);
}
@TypeConverter
public static Long toTimestamp(Date date){
return date == null ? null : date.getTime();
} 任何关于WHERE子句没有返回任何数据或指定行数的根本原因的帮助都将不胜感激。
发布于 2020-05-29 07:31:38
函数datetime('now', '-1 year')返回的时间戳格式为‘%Y-%m-%d%H:%M:%S’,例如2019-05-28 23:29:42 (docs)。要获取自纪元以来的毫秒数,请改用1000 * strftime('%s', datetime('now', '-1 year'))。
https://stackoverflow.com/questions/62075961
复制相似问题