房间不返回重复 [英] Room not returning duplicates

查看:109
本文介绍了房间不返回重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经设置了一个会议室数据库,一切都很好,所以我可以进行查询和插入,删除等操作,但是没有问题,但是我遇到了ID希望通过其ID和重复项返回条目的情况应该允许,但是房间正在删除重复项,因此,例如,我向它发送了一个ID列表,说< 1,2,3,2,3>,它按ID返回项目,但仅向我发送< 1,2 ,3>删除重复的条目.我正在执行的查询如下(在SQL中,顺便说一句完整的菜鸟)

So I have a room database all set up, everything is fine, so I can make queries and inserts, delete etc no problems, however i've just run into a situation where id like to return entries by their Ids and duplicates should be allowed, however room is removing the duplicates, so for instance I send it a list of ids say <1,2,3,2,3> and it returns items by their ids but only sends me <1,2,3> removing the duplicate entries. The query I'm making is below (btw complete noob at sql)

@Query("SELECT * FROM card WHERE cardId IN(:cardId)")
LiveData<List<Card>> getCardsByIds(List<Integer> cardId);

我通过我创建的存储库(只是一个抽象级别)使用它,并从ViewModel调用此存储库,此ViewModel有一个可变的实时数据整数列表,其中包含ID,并使用SwitchMap获得最新的实时数据.请包括以下相关内容

Im using it via a repository I created (just a level of abstraction) and calling this repo from a ViewModel, this ViewModel has a mutable live data integer list containing the ids and using a SwitchMap I get the latest live data. ill include the relevant pieces below

CARD REPO这样调用我的Daos方法

CARD REPO calls my Daos method like this

public LiveData<List<Card>> getCardsByIds(List<Integer> cardIds){
    return cardDao.getCardsByIds(cardIds);
}

ViewModel要求他们

ViewModel calls for them

private MutableLiveData<List<Integer>> cardIds;
//** constructor etc
cards = Transformations.switchMap(cardIds, id -> cardRepository.getCardsByIds(id));

,当cardIds列表更新时,通过SwitchMap的魔力,进行了新的查询,我观察了片段中的ViewModel.我已经调试了它,所以我知道Ids列表是正确的并且具有重复的Ids,但是返回的LiveData列表缺少重复的Ids.有帮助吗?

and through the magic of SwitchMap when the cardIds list updates a new query is made and I observe the ViewModel from my fragment. I've debugged it so I know the list of Ids is correct and has the duplicates Ids, but the returned LiveData list is missing the duplicate Ids. any help?

推荐答案

SQLiteDatabase始终以类似于SQL数据库的表格式将结果显示为Cursor.

The SQLiteDatabase always presents the results as a Cursor in a table format that resembles that of a SQL database.

来源:在查询返回结果之前,结果将存储在表中,并且如果存在具有重复主键的行,则默认情况下将省略它们.

Before the results are returned by the query, the results are stored in a table, and if there are rows with duplicate primary keys, they would be omitted by default.

要实现您想要的目标,可以执行查询以按ID循环查找单个元素并将结果附加到列表中.

To achieve what you intend, you can execute a query to find single element by id in loop and append the result to a list.

更新的DAO方法:

Updated DAO method:

@Query("SELECT * FROM card WHERE cardId=:cardId")
LiveData<Card> getCardById(Integer cardId);

更新存储库方法:

Update Repository method:

public LiveData<List<Card>> getCardsByIds(List<Integer> cardIds){
    List list = new ArrayList();
    for(Integer cardId: cardIds){
      list.append(cardDao.getCardById(cardId));
    }
    return list;
}

希望这会有所帮助.

原始答案:

如果id是模型的主键,则不允许输入重复的数据.因此,在检索时,您可能会发现缺少重复项.

If id is the primary key of your model, It doesn't allow duplicate data to be entered. Hence while retrieving you might find duplicates missing.

如果您有重复的id,请为主键创建另一个属性. (如果您没有任何主键属性,请使用自动生成)

If you have id with duplicate, create another attribute for primary key. (use autogenerate if you don't have any primary key attribute)

默认情况下,主键是UNIQUE且NOT NULL.

A primary key is by default UNIQUE and NOT NULL.

这篇关于房间不返回重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆