我不明白为什么我必须直接将代码放入 SQLiteOpenHelper 的 onCreate 中

发布于 2025-01-11 17:37:29 字数 5996 浏览 0 评论 0原文

我已经修复了代码中的一个问题,该问题不允许我将记录添加到 SQLite 数据库中。尽管我已经修复了它,但我并没有真正明白我的第一个代码有什么问题,所以如果有人愿意帮助我理解,请解释一下!

我的第一个代码:

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

我的新代码:

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

使用 updateMyDatabase(SQLiteDatabase db, int oldVersion, int newVersion) 的全部意义在于,我可以用一种方法管理数据库中的更改,正如我所关注的书中所述:< em>Head First Android 开发,第二版。问题是数据库已创建,但它是空的,因此 insertDrink() 方法不起作用。我认为它应该有效。

为了解决这个问题,我尝试以传统方式进行操作,直接添加在 onCreate() 内部运行所需的所有代码。这解决了问题,但是当我跟踪第一个代码如何运行时,我真的不认为结果应该有任何不同。谁能解释一下吗?

顺便说一句,如果您需要任何其他信息或代码,我非常愿意提供任何信息。 我提前为我的英语感到抱歉,它不是我的母语。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断爱 2025-01-18 17:37:29

在第一个代码中,您在尝试向其中插入数据后添加 FAVORITE 列。在第二个代码中,您首先创建包含 FAVORITE 列的表,然后插入数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文