首页 > 解决方案 > 带有 ID 的房间查询返回 null

问题描述

我正在为我的数据库使用房间持久性库。我的一个查询返回 null,我不明白为什么:

实体:

@Entity
public class Item{

    @PrimaryKey (autoGenerate = true)
    @ColumnInfo (name = "I_ID")
    private final int iId;

    @ColumnInfo (name = "I_Name")
    private final String iName;

    public int getIId() {
        return iId;
    }

    public String getIName() {
        return iName;
    }

    public Item(int iId, String iName) {
        this.iId = iId;
        this.iName = iName;
    }
}

道:

@Dao
public interface ItemDao {
    @Query("SELECT * FROM Item WHERE I_ID = :iId")
    Item getItemById(int iId);
}

此查询显示警告:Return value of the method is never used。它总是返回 null,即使我确定数据库中存在具有给定 ID 的项目。有人可以帮助我吗?

标签: androidandroid-room

解决方案


我发现尝试这样使用:

@Dao
public interface ItemDao {
@Query("SELECT * FROM item WHERE I_ID = :iId")
List<Item> getItemById(int iId);
}

推荐阅读