Adding a second table in a database

自作多情 提交于 2019-12-02 12:51:13

I think you need to choose a new (higher) version number, and modify your onUpgrade method to delete routes if it exists as well. Of course you may not actually need to delete your existing tables (e.g. creating and altering may be enough), but doing so is consistent with your current implementation.

You will call the SQLiteOpenHelper constructor with the new version, and it will internally call onUpgrade, passing the old and new versions.

Instead of:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS notes");
        onCreate(db);
    }

I had to modify the method like the following:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS notes");
        db.execSQL("DROP TABLE IF EXISTS routes"); // line added
        onCreate(db);
    }

And I instead of:

private static final int DATABASE_VERSION = 2;

I changed it to:

private static final int DATABASE_VERSION = 3;

However, I'm not completely aware of the REAL affect the change to the database version had.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!