首页 > 解决方案 > 我无法映射此 SQLite 查询的结果。在java中可以做到这一点吗?

问题描述

DAO: 
@Query ("SELECT COUNT (created_at) 'number_of_occurrence', datetime (created_at, 'unixepoch', 'localtime') 'local_datetime' FROM 'transaction' GROUP BY datetime (created_at, 'unixepoch', 'localtime') ORDER BY created_at DESC ")
 List <List <WrapperTransactionModel>> groupByCreatedAt ();

结果图像

在此处输入图像描述

Repository:
public List<List<WrapperTransactionModel>>   groupByCreatedAt() {
    return this.mDataBase.groupByCreatedAt();
}

收到错误:错误:无法弄清楚如何从游标中读取此字段

为解决问题而创建的类

public class WrapperTransactionModel {

@ColumnInfo(name = "number_of_occurrence")
private List<String> numberOfOccurrence;

@ColumnInfo(name = "local_datetime")
private List<String> localDatetime;

隐藏的访问方法(get 和 set)

非常欢迎所有帮助!谢谢!

标签: javaandroidsqlitemobileandroid-room

解决方案


Room 可以存储和检索的类型/对象有限。它不能(直接)存储/或检索列表,因此为什么 Room 说它不知道如何处理提取的数据。

Room 可以存储字符串,也可以将字符串检索到字符串列表中。

因此,我相信您可能希望 WrapperTransactionModel 成为

class WrapperTransactionModel {
    @ColumnInfo(name = "number_of_occurrence")
    private String numberOfOccurrence;

    @ColumnInfo(name = "local_datetime")
    private String localDatetime;
    ....

随着 :-

@Query("SELECT COUNT (created_at) 'number_of_occurrence', datetime (created_at, 'unixepoch', 'localtime') 'local_datetime' FROM 'transaction' GROUP BY datetime (created_at, 'unixepoch', 'localtime') ORDER BY created_at DESC ")
List<WrapperTransactionModel> groupByCreatedAt ();

也许考虑一下类似于您使用数据的上述演示:-

public class MainActivity extends AppCompatActivity {

    TransactionDatabase db;
    AllDao dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = Room.databaseBuilder(this,TransactionDatabase.class,"transction.db")
                .allowMainThreadQueries()
                .build();
        dao = db.getDao();

        /*
            Add 15 rows of testing data where
                the first row will have a created_at datetime based upon 60 days ago
                the next 3 rows 30 days ago
                and the remaining 11 today
         */
        long second = 1000;
        long near_a_month = 60 * 60 * 24 * 30;
        long adjust = near_a_month * 2;
        for(int i=0; i < 15; i++) {
            if (i > 0 && i < 3) adjust = near_a_month;
            if (i > 3) adjust = 0;
            dao.insertTransaction(new Transaction(String.valueOf((System.currentTimeMillis() / second) - adjust)));
        }

        // Get the data using the Query
        List<WrapperTransactionModel> myTransactionModels = dao.groupByCreatedAt();

        // Write the extracted data out to the log (3 row counts 11,3 and 1 )
        for(WrapperTransactionModel wtm: myTransactionModels) {
            Log.d("WTMINFO","NumberOfOccurrences = " + wtm.getNumberOfOccurrence()+ " LocalDateTime = " +wtm.getLocalDatetime());
        }
    }
}
  • 为了简洁和方便,Note 在主线程上运行。

写入日志的结果:-

2021-04-22 19:57:06.738 D/WTMINFO: NumberOfOccurrences = 11 LocalDateTime = 2021-04-22 19:57:06
2021-04-22 19:57:06.739 D/WTMINFO: NumberOfOccurrences = 3 LocalDateTime = 2021-03-23 20:57:06
2021-04-22 19:57:06.739 D/WTMINFO: NumberOfOccurrences = 1 LocalDateTime = 2021-02-21 20:57:06

推荐阅读