Home > Mobile >  I don't get why I HAVE to directly put code in the onCreate of my SQLiteOpenHelper
I don't get why I HAVE to directly put code in the onCreate of my SQLiteOpenHelper

Time:03-07

I've fixed an issue in my code that doesn't allow me to add records to my SQLite database. Even though I've fixed it, I don't really get what's wrong with my first code, so if anyone is willing to help me understand please do explain!

My first code:

package com.example.starbuzz;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class StarbuzzDatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "DatabaseName";
    public static final int DATABASE_VERSION = 2;


    public StarbuzzDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        updateMyDatabase(db, 0, DATABASE_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        updateMyDatabase(db, oldVersion, newVersion);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onDowngrade(db, oldVersion, newVersion);
    }

    private void updateMyDatabase(SQLiteDatabase db, int oldVersion, int newVersion){
        if (oldVersion < 1){
            db.execSQL("CREATE TABLE DRINK ("  
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT,"  
                    "NAME TEXT,"  
                    "DESCRIPTION TEXT,"  
                    "IMAGE_RESOURCE_ID INTEGER);");

            insertDrink(db, "Latte", "Expresso and steamed milk", R.drawable.latte, false);
            insertDrink(db, "Cappuccino", "Expresso, hot milk, and a steamed milk foam", R.drawable.cappuccino, true);
            insertDrink(db, "Filter", "Highest quality beans roasted and brewed fresh", R.drawable.filter, false);
        }

        if (oldVersion < 2){
            db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC");
        }
    }

    public static void insertDrink(SQLiteDatabase db,
                                   String name,
                                   String description,
                                   int resourceID,
                                   boolean favorite){
        ContentValues drinkValues = new ContentValues();

        drinkValues.put("NAME", name);
        drinkValues.put("DESCRIPTION", description);
        drinkValues.put("IMAGE_RESOURCE_ID", resourceID);
        drinkValues.put("FAVORITE", favorite);

        db.insert("DRINK", null, drinkValues);
    }
}

My new code:

package com.example.starbuzz;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class StarbuzzDatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "DatabaseName";
    public static final int DATABASE_VERSION = 2;


    public StarbuzzDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        updateMyDatabase(db, 0, DATABASE_VERSION);
        db.execSQL("CREATE TABLE DRINK ("  
                "_id INTEGER PRIMARY KEY AUTOINCREMENT,"  
                "NAME TEXT,"  
                "DESCRIPTION TEXT,"  
                "IMAGE_RESOURCE_ID INTEGER);");

        //Due to update version 2
        db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC");

        insertDrink(db, "Latte", "Expresso and steamed milk", R.drawable.latte, false);
        insertDrink(db, "Cappuccino", "Expresso, hot milk, and a steamed milk foam", R.drawable.cappuccino, true);
        insertDrink(db, "Filter", "Highest quality beans roasted and brewed fresh", R.drawable.filter, false);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        updateMyDatabase(db, oldVersion, newVersion);
        if (oldVersion < 2){
            db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC");
        }
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onDowngrade(db, oldVersion, newVersion);
    }

    private void updateMyDatabase(SQLiteDatabase db, int oldVersion, int newVersion){
/*        if (oldVersion < 1){
            db.execSQL("CREATE TABLE DRINK ("  
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT,"  
                    "NAME TEXT,"  
                    "DESCRIPTION TEXT,"  
                    "IMAGE_RESOURCE_ID INTEGER);");

            insertDrink(db, "Latte", "Expresso and steamed milk", R.drawable.latte, false);
            insertDrink(db, "Cappuccino", "Expresso, hot milk, and a steamed milk foam", R.drawable.cappuccino, true);
            insertDrink(db, "Filter", "Highest quality beans roasted and brewed fresh", R.drawable.filter, false);
        }

        if (oldVersion < 2){
            db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC");
        }  */
    }

    public static void insertDrink(SQLiteDatabase db,
                                   String name,
                                   String description,
                                   int resourceID,
                                   boolean favorite){

        ContentValues drinkValues = new ContentValues();

        drinkValues.put("NAME", name);
        drinkValues.put("DESCRIPTION", description);
        drinkValues.put("IMAGE_RESOURCE_ID", resourceID);
        drinkValues.put("FAVORITE", favorite);

        db.insert("DRINK", null, drinkValues);
    }
}

The whole point of using updateMyDatabase(SQLiteDatabase db, int oldVersion, int newVersion) was so that I could manage the changes in the database in one method, as instructed in the book I'm following: Head First Android Development, 2rd Edition. Problem is that the database gets created, but it's empty so the insertDrink() methods aren't working. I think it should be working.

To fix the issue, I tried doing it the conventional way by adding all the code needed to run in onCreate() inside itself directly. This fixed the issue, but when I trace how the first code would run, I really don't think the result should be any different. Can anyone explain?

BTW if you need any additional info or code, I'm very much willing to give whatever. I'm sorry in advance for my English, it isn't my first language.

CodePudding user response:

In your first code, you're adding the FAVORITE column after trying to insert data to it. In the second code, you create the table with the FAVORITE column first and then insert data.

  • Related