房间持久性库@Query无法正常工作

时间:2018-06-18 22:35:51

标签: android sqlite android-room

  

我还上传了the whole project to GitHub,代码和主要代码行,查询请求DetailFragment.java中的第144行。

我正在尝试使用id查询表中的成分,查询返回null结果。相同的查询运行良好但没有LIKE (:id) LIMIT 1,但是给出了所有行。

@Query("SELECT ingredients FROM recipe_db WHERE id LIKE (:id) LIMIT 1")
String getRecipeIngredients(List<Integer> id);

我试过了:

  1. =代替LIKE
  2. :ids没有()
  3. Integer/int id
  4. 如果所需的ID不存在,则检查数据库本身。
  5. recipe_db代码

    @Entity(tableName = DATABASE_NAME)
    public class Recipe implements Parcelable {
    
    @ColumnInfo(name = "image_url")
    private String image;
    
    @ColumnInfo(name = "servings")
    private int servings;
    
    @ColumnInfo(name = "steps")
    private List<Steps> steps;
    
    @ColumnInfo(name = "ingredients")
    private List<Ingredient> ingredients;
    
    @NonNull
    @ColumnInfo(name = "name")
    private String name;
    
    @PrimaryKey
    @ColumnInfo(name = "id")
    private int id;
    
    public Recipe(@NonNull String image, int servings, List<Steps> steps
            , List<Ingredient> ingredients, @NonNull String name, int id) {
        this.image = image;
        this.servings = servings;
        this.steps = steps;
        this.ingredients = ingredients;
        this.name = name;
        this.id = id;
    }
    
    
    public String getImage() {
        return image;
    }
    
    public int getServings() {
        return servings;
    }
    
    public List<Steps> getSteps() {
        return steps;
    }
    
    public List<Ingredient> getIngredients() {
        return ingredients;
    }
    
    public String getName() {
        return name;
    }
    
    public int getId() {
        return id;
    }
    
    public static class Steps {
        @SerializedName("thumbnailURL")
        private String thumbnailurl;
        @SerializedName("videoURL")
        private String videourl;
        @SerializedName("shortDescription")
        private String shortdescription;
        private String description;
        private int id;
    
        public String getThumbnailurl() {
            return thumbnailurl;
        }
    
        public String getVideourl() {
            return videourl;
        }
    
        public String getDescription() {
            return description;
        }
    
        public String getShortdescription() {
            return shortdescription;
        }
    
        public int getId() {
            return id;
        }
    
        public void setThumbnailurl(String thumbnailurl) {
            this.thumbnailurl = thumbnailurl;
        }
    
        public void setVideourl(String videourl) {
            this.videourl = videourl;
        }
    
        public void setShortdescription(String shortdescription) {
            this.shortdescription = shortdescription;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }
    
    public static class Ingredient {
        private String ingredient;
        private String measure;
    
        @Ignore
        private float quantity;
    
        private String str_quantity;
    
        public String getIngredient() {
            return ingredient;
        }
    
        public String getMeasure() {
            return measure;
        }
    
        public float getQuantity() {
            return quantity;
        }
    
        public void setIngredient(String ingredient) {
            this.ingredient = ingredient;
        }
    
        public void setMeasure(String measure) {
            this.measure = measure;
        }
    
        public void setQuantity(float quantity) {
            this.quantity = quantity;
        }
    
        public String getStr_quantity() {
            return String.valueOf(quantity);
        }
    
        public void setStr_quantity(String str_quantity) {
            this.str_quantity = str_quantity;
        }
    }
    
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.image);
        dest.writeInt(this.servings);
        dest.writeList(this.steps);
        dest.writeList(this.ingredients);
        dest.writeString(this.name);
        dest.writeInt(this.id);
    }
    
    protected Recipe(Parcel in) {
        this.image = in.readString();
        this.servings = in.readInt();
        this.steps = new ArrayList<Steps>();
        in.readList(this.steps, Steps.class.getClassLoader());
        this.ingredients = new ArrayList<Ingredient>();
        in.readList(this.ingredients, Ingredient.class.getClassLoader());
        this.name = in.readString();
        this.id = in.readInt();
    }
    
    public static final Parcelable.Creator<Recipe> CREATOR = new Parcelable.Creator<Recipe>() {
        @Override
        public Recipe createFromParcel(Parcel source) {
            return new Recipe(source);
        }
    
        @Override
        public Recipe[] newArray(int size) {
            return new Recipe[size];
        }
    };
    }
    

2 个答案:

答案 0 :(得分:0)

尝试这种方式查询..

@Query("SELECT ingredients FROM recipe_db WHERE id LIKE :id LIMIT 1")
String getRecipeIngredients(List<Integer> id);

如果你想,那么试试名字和其他字段。

答案 1 :(得分:0)

我已经在Android Nanodegree课程中问了我的导师“ ANTHONY”,这里是他的答案

  

发生这种情况是因为SQL设置不正确。

     

@Query(“从配方ID为WHERE id LIKE的ID中选择ID:ID LIMIT 1”)

     

您想要对其进行设置,使其与传入的ID相匹配。 LIKE子句   工作原理不同,并使用pattern matching symbols.