我无法映射此SQLite查询的结果。在java中可以做到这一点吗? DAO: @查询(“按日期时间从“transaction”组中选择计数(在“unixepoch”、“localtime”处创建的计数)、日期时间(在“unixepoch”、“localtime”处创建的计数)、本地日期时间(在“unixepoch”、“localtime”处创建的计数),按描述创建的顺序) 列表groupByCreatedAt();
结果图像我无法映射此SQLite查询的结果。在java中可以做到这一点吗? DAO: @查询(“按日期时间从“transaction”组中选择计数(在“unixepoch”、“localtime”处创建的计数)、日期时间(在“unixepoch”、“localtime”处创建的计数)、本地日期时间(在“unixepoch”、“localtime”处创建的计数),按描述创建的顺序) 列表groupByCreatedAt();,java,android,sqlite,mobile,android-room,Java,Android,Sqlite,Mobile,Android Room,结果图像 存储库: 公共列表groupByCreatedAt(){ 返回此.mDataBase.groupByCreatedAt(); } 收到错误: 错误:无法确定如何从光标读取此字段 为解决问题而创建的类 Repository: public List<List<WrapperTransactionModel>> groupByCreatedAt() { return this.mDataBase.groupByCreatedAt(); } 公共类包
存储库:
公共列表groupByCreatedAt(){
返回此.mDataBase.groupByCreatedAt();
}
收到错误:
错误:无法确定如何从光标读取此字段
为解决问题而创建的类
Repository:
public List<List<WrapperTransactionModel>> groupByCreatedAt() {
return this.mDataBase.groupByCreatedAt();
}
公共类包装器TransactionModel{
@ColumnInfo(name=“发生次数”)
私人名单号码;
@ColumnInfo(name=“local\u datetime”)
私有列表localDatetime;
隐藏访问方法(get和set)
欢迎大家帮忙!
谢谢!文件室可以存储和检索的类型/对象有限。它无法(直接)存储/检索列表,因此文件室说它不知道如何处理提取的数据 文件室可以存储字符串,也可以将字符串检索到字符串列表中 因此,我相信您可能希望WrapperTransactionModel
public class WrapperTransactionModel {
@ColumnInfo(name = "number_of_occurrence")
private List<String> numberOfOccurrence;
@ColumnInfo(name = "local_datetime")
private List<String> localDatetime;
以及:-
class WrapperTransactionModel {
@ColumnInfo(name = "number_of_occurrence")
private String numberOfOccurrence;
@ColumnInfo(name = "local_datetime")
private String localDatetime;
....
早上好@Mike T!我最终选择了另一种对我非常有效的方法。不过,非常感谢你的帮助!现在我要睡觉了。
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());
}
}
}
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