房间数据库无法 return group_concat 列
Room database fails to return group_concat column
我正在尝试在房间中创建一个 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);
}
视图已按预期创建,但是,对视图的查询 returns 所有 groupNames 都为空。我已经使用数据库浏览器检查了查询,其中 运行 对 sqlite 本身的查询。 DB Browser returns 预期数据。包含 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();
}
});
}
SiteDetailDao_Impl
的发布表明未生成代码来获取结果列 group_concat(groupName, ', ')
的值。我猜这是因为它没有简单的名字。因此,正如@sergiytikonov 在他的评论中建议的那样,您需要为该列提供一个别名。类似于:
group_concat(groupName, ', ') AS concatName
要支持新别名,请创建另一个 class(随意命名):
public class SiteInfo {
long memberId;
String address;
String concatName;
}
并将您的查询更改为 return 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);
我正在尝试在房间中创建一个 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);
}
视图已按预期创建,但是,对视图的查询 returns 所有 groupNames 都为空。我已经使用数据库浏览器检查了查询,其中 运行 对 sqlite 本身的查询。 DB Browser returns 预期数据。包含 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();
}
});
}
SiteDetailDao_Impl
的发布表明未生成代码来获取结果列 group_concat(groupName, ', ')
的值。我猜这是因为它没有简单的名字。因此,正如@sergiytikonov 在他的评论中建议的那样,您需要为该列提供一个别名。类似于:
group_concat(groupName, ', ') AS concatName
要支持新别名,请创建另一个 class(随意命名):
public class SiteInfo {
long memberId;
String address;
String concatName;
}
并将您的查询更改为 return 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);