首页 > 解决方案 > 带有 strftime 的 Room SQLite 选择语句不返回任何结果

问题描述

我有一个带有基于房间库的数据库的 android 应用程序。

public class Entry {

    public Entry(int cat_id, Date date, float value, String comment) {
        this.date = date;
        this.value = value;
        this.cat_id = cat_id;
        this.comment = comment;
    }
    @PrimaryKey(autoGenerate = true)
    public int entry_id;

    public final Date date;

    public final float value;

    public final int cat_id;

    public final String comment;
}

在 EntryDao 中,我有两个不起作用的 select 语句:

@Query(("SELECT sum(value) FROM Entry WHERE cat_id = :catId AND strftime('%Y', date) LIKE :year"))
float getSumByCatIdAndYear(int catId, int year);

@Query(("SELECT sum(value) FROM Entry WHERE cat_id = :catId AND strftime('%Y', date) LIKE :year AND strftime('%m', date) LIKE :month"))
float getSumByCatIdAndYearAndMonth(int catId, int year, int month);

我不知道为什么,但它总是返回 0.0 我认为我对 strftime 函数做错了,但我不确定。

标签: androidsqliteandroid-room

解决方案


推荐阅读