首页 > 解决方案 > 如何在android的房间db中获取映射到pojo的列信息

问题描述

I m getting below warning and query not working

 @Query("SELECT emailId FROM table_contacts")
    public LiveData<List<ContactsEntity>> getAllEmailIdFromDB();

my entity:
@PrimaryKey
    @NonNull
    @ColumnInfo(name = "contactId")
    private String contactId;
    @ColumnInfo(name = "phoneNumber")
    private String phoneNumber = null;
    @ColumnInfo(name = "emailId")
    private String emailId = null;
    @ColumnInfo(name = "name")
    private String name;
    @ColumnInfo(name = "isContactsSynched")
    private String isContactsSynched = "false";
    @ColumnInfo(name = "isRegistered")
    private String isRegistered = "false";

warning:
ContactsEntityDao.java:35: warning:  com.enstage.wibmo.main.db.entities.ContactsEntity has some fields [contactId, phoneNumber, name, isContactsSynched, isRegistered] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: emailId. Fields in com.enstage.wibmo.main.db.entities.ContactsEntity: contactId, phoneNumber, emailId, name, isContactsSynched, isRegistered.

我已经为所有字段添加了列信息,但我仍然收到警告和查询不起作用。

我想读取映射到 pojo 的列电子邮件 ID 和名称。如何实现这一点 任何帮助表示赞赏

标签: android

解决方案


如果您只需要电子邮件,您可以通过以下方式获取字符串列表:

@Query("SELECT emailId FROM table_contacts")
public LiveData<List<String>> getAllEmailIdFromDB();

如果您需要来自 DAO 的整个 POJO,您必须在请求中声明所有字段:

@Query("SELECT * FROM table_contacts")
public LiveData<List<ContactsEntity>> getAllEmailIdFromDB();

推荐阅读