代码之家  ›  专栏  ›  技术社区  ›  Dasser Basyouni

room persistence library@query不工作

  •  0
  • Dasser Basyouni  · 技术社区  · 7 年前

    我也上传了 the whole project to GitHub ,代码和查询所在的请求的主代码行是144行 DetailFragment.java .

    我试图从ID为的表中查询成分,查询返回的结果为空。相同的查询运行良好,但没有 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不存在,则检查数据库本身。

    配方代码

    @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 回复  |  直到 7 年前
        1
  •  0
  •   Mobile Team ADR-Flutter    7 年前

    尝试此方式查询..

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

    如果你想,那就在名称和其他字段上试试。

        2
  •  0
  •   Dasser Basyouni    7 年前

    我在安卓纳米课程中问过我的导师“安东尼”,他的答案在这里

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

    @查询(“从配方数据库中选择成分,其中id-like:id-limit 1”)。

    您要设置它,使其与传入的ID相匹配。LIKE子句 工作方式和用途不同 pattern matching symbols.

    推荐文章