在Android房间中查询日期



我有一个带有列dueDate的表todo_db,我想运行一个查询来选择dueDate类似于给定日期的所有内容。

我在我的Dao 中写下了这个查询

@Query("SELECT * FROM todo_db WHERE dueDate LIKE :givenDate")
List<todoEnt> getGivenDate(Date givenDate);

为了使用它,我调用了一个数据库实例,我使用了Calendar的实例并使用了getTime((

Calendar c;
Date dateToday;
c = Calendar.getInstance();
dateToday = c.getTime();
datalist = db.Maindao().getGivenDate(dateToday);

我试过

SELECT * FROM todo_db WHERE dueDate = :givenDate

SELECT * FROM todo_db WHERE dueDate < (strftime('%s', 'now'))

我感谢你能提供的任何帮助。非常感谢!

编辑:

我有TypeConverter和dueDate是日期

@Entity(tableName = "todo_db")
public class todoEnt {
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo(name = "title")
public String todoTitle;
@ColumnInfo(name = "description")
public String todoDescription;
@ColumnInfo(name = "dueDate")
public Date todoDueDate;
}

public class Convertors {
@TypeConverter
public Date fromTimeStamp(Long value){
return value == null ? null : new Date(value);
}
@TypeConverter
public Long dateToTimestamp(Date date){
return date == null ? null : date.getTime();
}
}

在方法中使用long而不是date。请记住,内部房间将日期存储为长。

所以在你的DAO中,你应该有这样的东西。。。

@Query("SELECT * FROM todo_db WHERE dueDate = :givenDateInLong")
getGivenDate(long givenDateInLong)  

那么你的代码会像。。。

Calendar c;
Date dateToday;
c = Calendar.getInstance();
dateToday = c.getTime();
long dateTodayInLong = dateToday.getTime();
datalist = db.Maindao().getGivenDate(dateTodayInLong);

最新更新