本文介绍了在同一线程上打开/关闭SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发食谱书,并且正在实现此方法以将我的食谱插入数据库中。在 for循环中,我从多个EditText获得了成分的名称和数量,并将它们分别保存在 Ingredient.class 实例( newIngredient )中。然后,将实例插入数据库,并将其添加到ArrayList。以下如果条件 用于标题,时间和其他食谱的属性。最后,我还将食谱和标签实例插入亲戚数据库的表中,然后关闭数据库。

I'm developing a recipe book and I'm implementing this method to insert my Recipe in the Database. In the for cycle I get the ingredient's name and quantity from multiples EditText, saving each of them in an Ingredient.class instance (newIngredient). Then I insert the instance into the DB and add it to an ArrayList. The followings "if conditions" are for the title, time and other Recipe's attributes. Finally, I also insert Recipe and Tag instances in the relatives DB's tables and I close DB.

public void saveRecipe() {

    dbHelper = new DatabaseHelper(context);

    // creating new recipe from user input
    Ingredient newIngredient;
    String title, childIngredient, instruction, tag;
    int target, time, childQuantity, calories;
    int countIngredients = parentIngredientLayout.getChildCount();
    int countTags = chipGroup.getChildCount();
    ArrayList<Ingredient> ingredients = null;
    ArrayList<Tag> tags = null;
    View childViewIng = null;
    EditText childTextViewI = null;
    EditText childTextViewQ = null;

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

    //recipe fields settings
    if (photoPath1 == null)
        photoPath1 = "";

    if (photoPath2 == null)
        photoPath2 = "";

    if (photoPath3 == null)
        photoPath3 = "";

    if (titleText.getText().toString().isEmpty()) {
        title = "";
    } else {
        title = titleText.getText().toString();
    }

    if (targetNumber.getText().toString().isEmpty()) {
        target = 0;
    } else {
        target = Integer.parseInt(targetNumber.getText().toString());
    }

    if (timeNumber.getText().toString().isEmpty()) {
        time = 0;
    } else {
        time = Integer.parseInt(timeNumber.getText().toString());
    }

    if (instructionText.getText().toString().isEmpty()) {
        instruction = "";
    } else {
        instruction = instructionText.getText().toString();
    }

    if (caloriesNumber.getText().toString().isEmpty()) {
        calories = 0;
    } else {
        calories = Integer.parseInt(caloriesNumber.getText().toString());
    }

    if (tagName.getText().toString().isEmpty()) {
        tag = "";
    } else {
        tag = tagName.getText().toString();
    }

    Recipe newRecipe = new Recipe(title, photoPath1, photoPath2, photoPath3, instruction, target, time, calories, ingredients);
    Tag newTag = new Tag(tag);

    dbHelper.insertRecipe(newRecipe);
    dbHelper.insertTag(newTag);

    dbHelper.close(); }

我通过调试发现,在这种情况下,仅插入第一个成分。我试图将FOR移至代码末尾,但在那种情况下,将同时插入配方和标签,并且始终仅插入第一个成分。我认为问题与数据库的打开/关闭有关。有人可以帮我吗?
成分构造函数:

I found out by debugging that in this case is inserted only the first ingredient. I tried to move the FOR until the end of code, but in that case, are inserted both recipe and tag and always only the first ingredient. I think the problem is relative to the opening/closing of the DB. Can somebody help me?Ingredient constructor:

public Ingredient(String ingredient_name, int quantity) {

    this.ingredient_name = ingredient_name;
    this.quantity = quantity;
}

dbHelper.insertIngredient(newIngredient)方法:

dbHelper.insertIngredient(newIngredient) method:

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

}


推荐答案

好,谢谢您的评论,我们解决了问题:)

Ok, thanks to your comment we got the problem :)

您正在呼叫 .add(newIngredient)在使用 ArrayList< Ingredient>初始化的列表上;成分= null;

将其更改为

ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();

它将起作用:)

祝你好运!

这篇关于在同一线程上打开/关闭SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:35