android - 如何使用 Room 从光标中获取数据
问题描述
我正在使用此查询从数据库中获取数据,Room
但无法弄清楚此查询的返回类型以及如何从该查询中获取数据
@Query("SELECT COUNT(Unit), Age as COUNT from test where Age in (1, 2, 3) and Unit in ("U5", "U6", "U4") group by Age")
我在 sqllite online 中运行时得到的输出如下图所示。所以它就像键值对。
解决方案
步骤#1:修改查询以命名两个输出:
SELECT COUNT(Unit) as count, Age from test where Age in (1, 2, 3) and Unit in ("U5", "U6", "U4") group by Age
步骤#2:创建一个匹配查询输出的 POJO:
class AgeCounts {
public int count;
public int age;
}
第 3 步:让 DAO 方法(您@Query
将使用的方法)返回List
您的 POJO 类(例如,List<AgeCounts>
),可能包装在反应类型中(例如LiveData<List<AgeCounts>>
,,Single<List<AgeCounts>>
):
@Query("SELECT COUNT(Unit) as count, Age from test where Age in (1, 2, 3) and Unit in ("U5", "U6", "U4") group by Age")
List<AgeCounts> getAgeCounts();
推荐阅读
- x-cart - 如何从 X-Cart 中的数据库重置管理员用户访问权限?
- read-the-docs - 更新 readthedocs 上的分支列表
- java - 一次性登录应用 - FirebaseAuth
- python-3.x - 如何在 x 轴上指定日期以使用 Python 3 制作条形图
- angular - 没有导出成员“SubscriptionLike”
- html - 从谷歌表读取数据在客户端html中全局使用
- css - MaterialUI如何将输入内的文本向右或居中对齐?
- java - 为什么 contains(Object) 在 TreeMap 中是 log(n) 而在 PriorityQueue 中是 O(n),而 PriorityQueue 在内部使用二叉树(二叉堆)?
- autosar - Flexray COM 与应用程序的通信
- android - React Native:即使在应用程序退出后,谷歌地图服务仍会在 Android 设备中运行