英文:
Query with date in Android Room
问题
我有一个名为todo_db
的表,其中有一列dueDate
,我想运行一个查询来选择所有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
的类型是Date:
@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();
}
}
希望这对您有所帮助。
英文:
I have a table todo_db
with a column dueDate
and I want to run a query to select all where dueDate
is like a given date.
I wrote this query in my Dao
@Query("SELECT * FROM todo_db WHERE dueDate LIKE :givenDate")
List<todoEnt> getGivenDate(Date givenDate);
To use it I call for an instance of the database, I use an instance of Calendar and use getTime()
Calendar c;
Date dateToday;
c = Calendar.getInstance();
dateToday = c.getTime();
datalist = db.Maindao().getGivenDate(dateToday);
I have tried with
SELECT * FROM todo_db WHERE dueDate = :givenDate
and
SELECT * FROM todo_db WHERE dueDate < (strftime('%s', 'now'))
I appreciate any help you can give. Thanks a lot!
Edit:
I have TypeConverter and dueDate is Date
@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();
}
}
答案1
得分: 2
在方法中使用 long 而不是 date。请记住,Room 在内部将日期存储为 long。
因此,在您的 DAO 中应该有类似以下的内容...
@Query("SELECT * FROM todo_db WHERE dueDate = :givenDateInLong")
List<TodoItem> getGivenDate(long givenDateInLong)
然后您的代码将会是...
Calendar c;
Date dateToday;
c = Calendar.getInstance();
dateToday = c.getTime();
long dateTodayInLong = dateToday.getTime();
datalist = db.Maindao().getGivenDate(dateTodayInLong);
请注意,这些代码片段中的注释符号(")已经被删除,以便更好地呈现代码。
英文:
use long instead of date in method. remember that internally room is storing the date as a long.
so in your DAO you should have something like ...
@Query("SELECT * FROM todo_db WHERE dueDate = :givenDateInLong")
getGivenDate(long givenDateInLong)
then your code will be like ...
Calendar c;
Date dateToday;
c = Calendar.getInstance();
dateToday = c.getTime();
long dateTodayInLong = dateToday.getTime();
datalist = db.Maindao().getGivenDate(dateTodayInLong);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论