在Android Room中带有日期的查询

huangapple go评论65阅读模式
英文:

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(&quot;SELECT * FROM todo_db WHERE dueDate LIKE :givenDate&quot;)
    List&lt;todoEnt&gt; 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 &lt; (strftime(&#39;%s&#39;, &#39;now&#39;))

I appreciate any help you can give. Thanks a lot!

Edit:

I have TypeConverter and dueDate is Date

@Entity(tableName = &quot;todo_db&quot;)
public class todoEnt {
    @PrimaryKey(autoGenerate = true)
    public int id;

    @ColumnInfo(name = &quot;title&quot;)
    public String todoTitle;

    @ColumnInfo(name = &quot;description&quot;)
    public String todoDescription;

    @ColumnInfo(name = &quot;dueDate&quot;)
    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(&quot;SELECT * FROM todo_db WHERE dueDate = :givenDateInLong&quot;)
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);

huangapple
  • 本文由 发表于 2020年8月4日 10:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63239381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定