【问题标题】:Select multipile column through Room liberary通过房间库选择多列
【发布时间】:2019-02-13 11:44:57
【问题描述】:

我想在 Android SDK 环境中通过 Room Library 从 SqlLite 中选择多个列。 下面是选择它的查询。 @Query("SELECT ID,message,timestamp FROM Chat_Message WHERE groupID =:groupID ORDER BY 时间戳 DESC LIMIT 1") 公共列表 get_last_msg_ID_timestamp(字符串 组ID); 我在主类下定义的 Last_Msg_Detail 类如下:- 公共类 Last_Msg_Detail { 公共整数 ID; 公共字符串消息; 公共长时间戳; }

   For accessing this three variable have created below method :-
   Last_Msg_Detail last_record_t = new Last_Msg_Detail();
   public Last_Msg_Detail get_last_msgand_time_stamp(String groupID){
    List<Last_Msg_Detail> last_record = 
    chat_messageDao.get_last_msg_ID_timestamp(groupID);
    last_record_t = last_record.get(0);
    return last_record_t;
   } 
On Rebuilding Project, getting follow error 
 1. error: Cannot figure out how to save this field into database. You can 
 consider adding a type converter for it.
 2. error: Not sure how to convert a Cursor to this method's return type

Kindly advise how to resolve.

提前感谢您的帮助。

【问题讨论】:

    标签: android


    【解决方案1】:

    您的方法返回自定义对象,并且该对象有很多字段。

    因此,当您尝试返回特定列时,您会尝试返回一个新对象。所以会发生错误。

    要解决此问题,请为选定的列创建一个新对象。它必须有这些字段。

     ID,message,timestamp
    

    并在您的方法中使用该对象

    @Query("SELECT ID,message,timestamp FROM Chat_Message WHERE groupID =:groupID ORDER BY timestamp DESC LIMIT 1")
     public List<NEW_OBJECT> get_last_msg_ID_timestamp (String groupID); 
    

    【讨论】:

    • 感谢@Momen 的建议。将检查并返回。同时请您告知我们是否可以选择三列而不是通过对象方式?我们不能有更简单的方法来检索价值吗?谢谢
    • 对不起,我认为没有办法,也许你可以使用@TypeConverter 看到这个developer.android.com/training/data-storage/room/… 或者你可以选择 * 并返回 list 然后创建 getter 和 setter。所以你可以只使用 (getId, getMessage, getTimestamp) 方法
    猜你喜欢
    • 1970-01-01
    • 2013-01-29
    • 2019-05-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多