我无法Map此sqlite查询的结果在java中可以做到这一点吗?

ee7vknir  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(430)
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)
欢迎大家帮忙!谢谢!

shstlldc

shstlldc1#

文件室的可存储和检索类型/对象有限。它不能(直接)存储/检索列表,因此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 ();

或许可以考虑一下上面的演示,它类似于您使用data:-

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

注意:为了简洁和方便,在主线程上运行。
写入日志的结果:-

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

相关问题