首页 > 解决方案 > 通过房间库选择多桩柱

问题描述

我想在 Android SDK 环境中通过 Room Library 从 SqlLite 中选择多个列。下面是选择它的查询。@Query("SELECT ID,message,timestamp FROM Chat_Message WHERE groupID =:groupID ORDER BY timestamp DESC LIMIT 1") public List get_last_msg_ID_timestamp (String groupID); 我在主类下定义的 Last_Msg_Detail 类如下:- public class Last_Msg_Detail { public Integer 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

解决方案


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

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

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

 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); 

推荐阅读