Android 在SQL数据库的一行中插入ArrayList
在我的食谱日记中,我有3节课,Recipe.class:Android 在SQL数据库的一行中插入ArrayList,android,sql,database,arraylist,Android,Sql,Database,Arraylist,在我的食谱日记中,我有3节课,Recipe.class: public Recipe(String title, String firstImage, String secondImage, String thirdImage, String instructions, int targetPeople, int time, int calories, ArrayList<Ingredient> ingredients, ArrayList<Tag> tags) {
public Recipe(String title, String firstImage, String secondImage, String thirdImage, String instructions, int targetPeople, int time, int calories, ArrayList<Ingredient> ingredients, ArrayList<Tag> tags) {
this.title = title;
this.firstImage = firstImage;
this.secondImage = secondImage;
this.thirdImage = thirdImage;
this.instructions = instructions;
this.targetPeople = targetPeople;
this.time = time;
this.calories = calories;
this.ingredients = ingredients;
this.tags = tags;
}
和Tag.class:
public Tag(String tag_name) {
this.tag_name = tag_name;
}
当我保存一个新配方时,我使用两个for循环来存储标签和配料,每个标签和配料都被添加到相应的ArrayList中,以将更多配料和标签链接到同一配方,如下所示:
for (int c=0; c < countTags; c++) {
childChipView = (Chip) chipTagGroup.getChildAt(c);
childTextViewTag = childChipView.findViewById(R.id.chip_item);
childTag = childTextViewTag.getText().toString();
newTag = new Tag(childTag);
dbHelper.insertTag(newTag);
tags.add(newTag);
}
// ingredients fields settings
for (int d=0; d<countIngredients; d++) {
childViewIng = parentIngredientLayout.getChildAt(d);
childTextViewI = childViewIng.findViewById(R.id.ingredientsField);
childTextViewQ = childViewIng.findViewById(R.id.quantityField);
childIngredient = childTextViewI.getText().toString();
childQuantity = Integer.parseInt(childTextViewQ.getText().toString());
newIngredient = new Ingredient(childIngredient, childQuantity);
dbHelper.insertIngredient(newIngredient);
ingredients.add(newIngredient);
}
之后,我存储在DB配方表、配料表、标签表的三个对应表中。我的问题是,如何将这两个ArrayList存储在RECIPE_表中?我想要,即,在该表的同一行中,标题、说明、时间、卡路里等以及两个ArrayList您不应该在一行中插入整个数组,因为这违反了1NF,尝试将其拆分为两个表根据定义ArrayList将是数据库中具有一对多关系的另一个表。。。当然,您可以序列化ArrayList并将其存储为字符串,但这会很尴尬,因此您建议我创建一个RECIPE_Component表和RECIPE_TAG表,以将ArrayList与同一个RECIPE关联?RECIPES ID、Name。。。TAGSID,Name,。。IngreditSID,名称,。。配方\u TAGSRecipeID,TagID配方\u IngCreditsRecipeId,IngCreditId。。。这就是大多数程序员使用关系数据库的方式
for (int c=0; c < countTags; c++) {
childChipView = (Chip) chipTagGroup.getChildAt(c);
childTextViewTag = childChipView.findViewById(R.id.chip_item);
childTag = childTextViewTag.getText().toString();
newTag = new Tag(childTag);
dbHelper.insertTag(newTag);
tags.add(newTag);
}
// ingredients fields settings
for (int d=0; d<countIngredients; d++) {
childViewIng = parentIngredientLayout.getChildAt(d);
childTextViewI = childViewIng.findViewById(R.id.ingredientsField);
childTextViewQ = childViewIng.findViewById(R.id.quantityField);
childIngredient = childTextViewI.getText().toString();
childQuantity = Integer.parseInt(childTextViewQ.getText().toString());
newIngredient = new Ingredient(childIngredient, childQuantity);
dbHelper.insertIngredient(newIngredient);
ingredients.add(newIngredient);
}