我正在尝试在室内创建DatabaseView,以从多个表中获取所需的数据。这是我的数据库视图:
@DatabaseView("SELECT site.name AS address, group_site.name AS groupName, group_site.member_id AS memberId " +
"FROM site, group_site " +
"INNER JOIN groupsite_join_site " +
"ON site.id = groupsite_join_site.site_id AND group_site.id = groupsite_join_site.group_site_id " +
"UNION SELECT preloaded_site.name AS address, preloaded_group_site.name AS groupName, preloaded_group_site.memberId AS memberId " +
"FROM preloaded_site, preloaded_group_site " +
"INNER JOIN preloaded_groupsite_join_site ON preloaded_site.id = preloaded_groupsite_join_site.site_id AND preloaded_site.id = preloaded_groupsite_join_site.site_id"
)
public class SiteDetail {
long memberId;
String address;
String groupName;
}
这是我的DAO:
@Dao
public interface SiteDetailDao {
@Query("SELECT address, group_concat(groupName, ', '), memberId FROM sitedetail WHERE memberId = :memberId OR memberId = -1 GROUP BY address LIMIT 10")
LiveData<List<SiteDetail>> getSiteDetailForMember(long memberId);
}
该视图按预期方式创建,但是视图上的查询将所有groupNames返回为null。我已经检查了使用数据库浏览器查询,该查询在sqlite本身上运行。数据库浏览器返回期望的数据。包含10条记录的列表,每条记录如下:
|id| address | groupName |
| 1|youtube.com|videos, entertainment, streaming|
为什么会这样?房间的group_contact是否有问题?这是SiteDetailDao_Impl:
@Override
public LiveData<List<SiteDetail>> getSiteDetailForMember(final long memberId) {
final String _sql = "SELECT address, group_concat(groupName, ', '), memberId FROM sitedetail WHERE memberId = ? OR memberId = -1 GROUP BY address LIMIT 10";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
int _argIndex = 1;
_statement.bindLong(_argIndex, memberId);
return __db.getInvalidationTracker().createLiveData(new String[]{"sitedetail"}, false, new Callable<List<SiteDetail>>() {
@Override
public List<SiteDetail> call() throws Exception {
final Cursor _cursor = DBUtil.query(__db, _statement, false);
try {
final int _cursorIndexOfAddress = CursorUtil.getColumnIndexOrThrow(_cursor, "address");
final int _cursorIndexOfMemberId = CursorUtil.getColumnIndexOrThrow(_cursor, "memberId");
final List<SiteDetail> _result = new ArrayList<SiteDetail>(_cursor.getCount());
while(_cursor.moveToNext()) {
final SiteDetail _item;
_item = new SiteDetail();
final String _tmpAddress;
_tmpAddress = _cursor.getString(_cursorIndexOfAddress);
_item.setAddress(_tmpAddress);
final long _tmpMemberId;
_tmpMemberId = _cursor.getLong(_cursorIndexOfMemberId);
_item.setMemberId(_tmpMemberId);
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
}
}
@Override
protected void finalize() {
_statement.release();
}
});
}
答案 0 :(得分:1)
发布SiteDetailDao_Impl
表示未生成代码来获取结果列group_concat(groupName, ', ')
的值。我猜这是因为它没有简单的名称。因此,正如@sergiytikonov在其评论中建议的那样,您需要为该列提供别名。像这样:
group_concat(groupName, ', ') AS concatName
要支持新别名,请创建另一个类(根据需要命名):
public class SiteInfo {
long memberId;
String address;
String concatName;
}
并更改查询以返回LiveData<List<SiteInfo>>
:
@Query("SELECT address, group_concat(groupName, ', ') AS concatName, memberId FROM sitedetail'" +
" WHERE memberId = :memberId OR memberId = -1 GROUP BY address LIMIT 10")
LiveData<List<SiteInfo>> getSiteDetailForMember(long memberId);