简体   繁体   English

Android机房SELECT * WHERE返回所有记录

[英]Android room SELECT * WHERE returns all records

I've got an Android Room database setup and followed some tutorials with which I'm trying to extend what I've made. 我已经完成了Android Room数据库的设置,并遵循了一些教程,以尝试扩展所做的工作。 I have gotten a few queries to work but when I try to use a SELECT * WHERE query it returns all the records in the table not just the records with the expected ID. 我得到了一些查询,但是当我尝试使用SELECT * WHERE查询时,它返回表中的所有记录,而不仅仅是具有期望ID的记录。

The Query in the DAO DAO中的查询

@Query("SELECT * FROM weapon_table WHERE fighter_id = :fighter_id" )
LiveData<List<Weapon>> getFighterWeapons(long fighter_id);

The Record Object 记录对象

@Entity(tableName = "weapon_table",
        foreignKeys = {
                @ForeignKey(entity = com.Database.Fighter.class,
                        parentColumns = "fighter_id",
                        childColumns = "fighter_id"),
        })
public class Weapon
{
    @NonNull
    String weapon_name;
    @NonNull
    @PrimaryKey(autoGenerate = true)
    long weapon_id;
    @NonNull
    long fighter_id;


    public Weapon(String weapon_name, long fighter_id)
    {
        this.weapon_name = weapon_name;
        this.fighter_id = fighter_id;
    }

    public long getWeapon_id()
    {
        return weapon_id;
    }

    public void setWeapon_id(long weapon_id)
    {
        this.weapon_id = weapon_id;
    }

    public String getWeapon_name()
    {
        return weapon_name;
    }

    public void setWeapon_name(String weapon_name)
    {
        this.weapon_name = weapon_name;
    }

    public long getFighter_id()
    {
        return fighter_id;
    }

    public void setFighter_id(long fighter_id)
    {
        this.fighter_id = fighter_id;
    }
}

When I call the following code all records are printed to the debug console but the current ID is also printed and stays the same in all cases. 当我调用以下代码时,所有记录都会打印到调试控制台,但是当前ID也会被打印出来,并且在所有情况下都保持不变。

   public void onClickAddWeapon(View view)
{
    List<Weapon> weapons = mWeaponViewModel.getFighterWeapons(fighter.getFighter_id()).getValue();
    for(Weapon weapon: weapons )
    {
        Log.d("WEAPONDEBUG", "WEAPONNAME/ID: " +weapon.getWeapon_name() +'/'+ weapon.getFighter_id() );
        Log.d("WEAPONDEBUG", "FIGHTERID: "+ fighter.getFighter_id() );

    }
}

Fix was fairly simple , I had followed this google code labs tutorial and when I tried to extend it I forgot to actually assign the result of the query to the list held in the repository file. 修复非常简单,我遵循了此google代码实验室教程 ,当我尝试对其进行扩展时,我忘记了将查询结果实际分配给存储库文件中包含的列表。 Thanks for the suggestions they put me in the right direction. 感谢您的建议,他们使我朝着正确的方向前进。

  LiveData<List<Weapon>> getFighterWeapons(long fighter_id)
    {
        mWeaponDao.getFighterWeapons(fighter_id);
        return mAllWeapons;
    }

replaced with 替换为

LiveData<List<Weapon>> getFighterWeapons(long fighter_id)
{
    mAllWeapons = mWeaponDao.getFighterWeapons(fighter_id);
    return mAllWeapons;
}

your code should be working, but I think that the problem is defining with foreign keys. 您的代码应该可以正常工作,但是我认为问题是使用外键定义的。 ` `

parentColumns = "fighter_id",

childColumns = "fighter_id"

can you remove it and try? 您可以删除它并尝试吗?

                    `

Try this 尝试这个

@Query("SELECT * FROM weapon_table WHERE fighter_id IN (:fighter_id)" )
LiveData<List<Weapon>> getFighterWeapons(long fighter_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM