【问题标题】:SQLite Database for a RecipeApp (one-to-many relationships)RecipeApp 的 SQLite 数据库(一对多关系)
【发布时间】:2019-04-04 18:30:18
【问题描述】:

在我的应用程序中,我创建了一个 Recipe 类、一个成分类和一个 ListIngredient 类;然后在我的 DBHelper 中,我创建了一个食谱表、一个成分表和一个 ListIngredient 表,以将一个食谱链接到更多成分,以这种方式:

String RecipeTable = "CREATE TABLE " + TBL_RECIPE + " ( " +
            RECIPE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            RECIPE_TITLE + " TEXT, " +
            RECIPE_FIRST_PHOTO + " TEXT, " +
            RECIPE_SECOND_PHOTO + " TEXT, " +
            RECIPE_THIRD_PHOTO + " TEXT, " +
            RECIPE_TARGET + " INTEGER, " +
            RECIPE_TIME + " INTEGER, " +
            RECIPE_INSTRUCTIONS + " TEXT, " +
            RECIPE_CALORIES + " INTEGER, " +
            KEY_CREATED_AT + " DATETIME" + ")";
    db.execSQL(RecipeTable);


String IngredientTable = "CREATE TABLE " + TBL_INGREDIENTS + " ( " +
            INGREDIENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            INGREDIENT_NAME + " TEXT, " +
            QUANTITY + " INTEGER, " +
            KEY_CREATED_AT + " DATETIME" + ")";
    db.execSQL(IngredientTable);

String ListIngredients = "CREATE TABLE " + TBL_LIST_INGREDIENTS + " ( " +
            INGREDIENT_ID + " INTEGER, " +
            INGREDIENT_NAME + " TEXT, " +
            RECIPE_ID + " INTEGER," +
            " FOREIGN KEY ("+RECIPE_ID+") REFERENCES "+TBL_RECIPE+"("+RECIPE_ID+"));";
    db.execSQL(ListIngredients);

然后我用这些方法插入一个新配方和一个新成分:

public boolean insertRecipe(Recipe recipe) {
    db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(RECIPE_TITLE, recipe.getTitle());
    contentValues.put(RECIPE_FIRST_PHOTO, recipe.getFirstImage());
    contentValues.put(RECIPE_SECOND_PHOTO, recipe.getSecondImage());
    contentValues.put(RECIPE_THIRD_PHOTO, recipe.getThirdImage());
    contentValues.put(RECIPE_TARGET, recipe.getTargetPeople());
    contentValues.put(RECIPE_TIME, recipe.getTime());
    contentValues.put(RECIPE_INSTRUCTIONS, recipe.getInstructions());
    contentValues.put(RECIPE_CALORIES, recipe.getCalories());
    contentValues.put(KEY_CREATED_AT, time.getTime().toString());

    long result = db.insert(TBL_RECIPE, null, contentValues);
    //db.close();
    Log.e(TAG, "Recipe inserted!");

    if (result == -1) {
        return false;
    } else {
        return true;
    }

}

public boolean insertIngredient(Ingredient ingredient) {

    db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(INGREDIENT_NAME, ingredient.getIngredient_name());
    contentValues.put(QUANTITY, ingredient.getQuantity());
    contentValues.put(KEY_CREATED_AT, time.getTime().toString());

    long result = db.insert(TBL_INGREDIENTS, null, contentValues);
    //db.close();
    Log.e(TAG, "Ingredient inserted!");

    if (result == -1) {
        return false;
    } else {
        return true;
    }

}

但是如何将元素插入到 ListIngredient 表中?

【问题讨论】:

    标签: android database sqlite


    【解决方案1】:

    只需对其他表执行相同的操作,但列名不同:

    public boolean insertListIngredient(ListIngredient listIngredient) {
    
        db = this.getWritableDatabase();
    
        ContentValues contentValues = new ContentValues();
        contentValues.put(INGREDIENT_ID, listIngredient.getIngredientId());
        contentValues.put(INGREDIENT_NAME, listIngredient.getIngredientName());
        contentValues.put(RECIPE_ID, listIngredient.getReceipeId());
    
        long result = db.insert(TBL_LIST_INGREDIENTS, null, contentValues);
        //db.close();
        Log.e(TAG, "ListIngredient inserted!");
    
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    
    }
    

    Obv 因为我没有你的类,所以我随机编写了 getter,只需用正确的值更改它们就可以了

    编辑:

    db.insert(...) 方法返回插入行的 ID,references。所以你可以把它返回并传递给你的下一个方法,我在这里写 Receipe 表的例子:

    来自参考:

    返回

    长新插入行的行ID,如果发生错误则为-1

    插入并返回:

    public long insertRecipe(Recipe recipe) {
        db = this.getWritableDatabase();
    
        ContentValues contentValues = new ContentValues();
        contentValues.put(RECIPE_TITLE, recipe.getTitle());
        contentValues.put(RECIPE_FIRST_PHOTO, recipe.getFirstImage());
        contentValues.put(RECIPE_SECOND_PHOTO, recipe.getSecondImage());
        contentValues.put(RECIPE_THIRD_PHOTO, recipe.getThirdImage());
        contentValues.put(RECIPE_TARGET, recipe.getTargetPeople());
        contentValues.put(RECIPE_TIME, recipe.getTime());
        contentValues.put(RECIPE_INSTRUCTIONS, recipe.getInstructions());
        contentValues.put(RECIPE_CALORIES, recipe.getCalories());
        contentValues.put(KEY_CREATED_AT, time.getTime().toString());
    
        long result = db.insert(TBL_RECIPE, null, contentValues);
        //db.close();
        Log.e(TAG, "Recipe inserted!");
    
        return result;
    
    }
    

    方法调用(示例):

    Receipe mReceipe;
    ListIngredient mListIngredient;
    
    // all code
    long receipeId = dbHelper.insertRecipe(mReceipe);
    if(receipeId != -1){
        mListIngredient.setReceipeId(receipeId);
        dbHelper.insertListIngredient(mListIngredient);
    }
    

    【讨论】:

    • 我试过了,但是在我的 AddRecipeActivity 类中,我有一个插入配料和食谱的方法(你可以在这里看到它:stackoverflow.com/questions/55379349/…)而且我不知道如何存储食谱的 ID和成分
    • @EdoardoTavilla 已编辑,如果清楚请告诉我 :)
    • 你是英雄! :)
    • @EdoardoTavilla 不客气,顺便看看realm.. 我真的很喜欢它用于数据库处理
    猜你喜欢
    • 1970-01-01
    • 2011-07-30
    • 2015-11-11
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2015-11-05
    • 2014-08-18
    • 1970-01-01
    相关资源
    最近更新 更多