用于RecipeApp的SQLite数据库(一对多关系)

时间:2019-04-04 10:00:37

标签: android database sqlite

在我的应用中,我创建了一个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表中?

1 个答案:

答案 0 :(得分:3)

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

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,因为我没有您的课程,所以我随机编写了吸气剂,只需将其更改为正确的值就可以了

编辑:

您的db.insert(...)方法返回插入的行references的ID。这样您就可以返回它并将其传递给您的下一个方法,我在这里为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);
}