在我的应用程序中,我创建了一个配方类、一个成分类和一个列表成分类;然后在dbhelper中,我创建了一个配方表、一个成分表和一个列表成分表,以便将一个配方链接到更多的成分,方法如下:

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;
    }

}

但是我怎样才能把元素插入到listcamentary表中呢?

最佳答案

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

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(...)方法返回插入行的idreferences。所以您可以返回它并将其传递给下一个方法,我在这里编写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);
}

关于android - 用于RecipeApp的SQLite数据库(一对多关系),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55513113/

10-10 09:02