在我的应用程序中,我创建了一个Recipe类,一个Ingredient类和一个ListIngredient类;然后在我的DBHelper中创建了一个Recipe表,一个Ingredient表和一个ListIngredient表,用于将一个Recipe链接到更多成分,这样:
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表中的元素插入?
就像你已经为其他表做的一样,使用不同的列名:
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表的例子:
参考:
返回
long插入新插入行的行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);
}