onUpgrade() 在新安装的应用程序

onUpgrade() at newly installed app

我对 onUpgrade 有点困惑。我正在开发我的应用程序,在开发过程中我确实更改了 DATABASE_VERSION 五次,所以现在是 6。

一切都很顺利,直到我从 phone 中完全卸载应用程序并重新构建它。从那时起,我收到的错误是 none 我要求的表格存在...

我认为现在,当我的 DATABASE_VERSIONin 项目设置为 6 并且新安装的应用程序在启动时将其设置为 6 时,onUpgrade() 不会被调用。

问题是如何为新安装提供我在 onUpgrade 方法中设置的所有查询?

我知道 onUpgrade() 的名称与 getWritableDatabase() 的名称相同。但是我的应用程序在这条车道上崩溃了:

Cursor spinner_cursor = db.rawQuery("SELECT value1 AS _id, value2 FROM tab2", null);

前面是车道:

SQLiteDatabase db = new DBHelper(this).getWritableDatabase();

我在 logCat 中收到此错误:

no such table: car (code 1): , while compiling: SELECT value1 AS _id, value2 FROM tab2

我的DBHelper.class长得像

public class DBHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "DataBase1";
private static final int DATABASE_VERSION = 6; 
private static final String CREATE_QUERY =
        "CREATE TABLE tab1 (id, value1, value2, etc)";

private static final String QUERY_2 =
        "CREATE TABLE tab2 (id, value1, value2, etc)";

private static final String QUERY_3 =
        "CREATE TABLE tab3 (id, value1, value2, etc)";

private static final String QUERY_4 =
        "ALTER TABLE tab2 ADD COLUMN value3 VARCHAR(10) NULL";

private static final String QUERY_5 =
        "ALTER TABLE tab2 ADD COLUMN value4 VARCHAR(10) NULL";

private static final String QUERY_6 =
        "ALTER TABLE tab2 ADD COLUMN value5 VARCHAR(10) NULL";

private static final String QUERY_7 =
        "ALTER TABLE tab3 ADD COLUMN value3 VARCHAR(10) NULL"; 

private static final String QUERY_8 =
        "ALTER TABLE tab3 ADD COLUMN value4 VARCHAR(10) NULL";

private static final String QUERY_9 =
        "ALTER TABLE tab3 ADD COLUMN value5 VARCHAR(10) NULL"; 

private static final String QUERY_10 =
        "ALTER TABLE tab3 ADD COLUMN value6 VARCHAR(10) NULL"; 

private static final String QUERY_11 =
        "ALTER TABLE tab3 ADD COLUMN value7 VARCHAR(10) NULL"; 

private static final String QUERY_12 =
        "DROP TABLE tab3";

private static final String QUERY_13 =
        "CREATE TABLE tab3 (id, value1, value2, etc)";


public DBHelper(Context context){

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.e("DATABASE OPERATIONS", "Database created / opened");
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_QUERY);
}

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

    if (oldVersion < 3) {
        db.execSQL(QUERY_2);
        db.execSQL(QUERY_3);
        db.execSQL(QUERY_4);
        db.execSQL(QUERY_5);
        db.execSQL(QUERY_6);
    }

    if (oldVersion < 4) {
        db.execSQL(QUERY_7);
        db.execSQL(QUERY_8);
        db.execSQL(QUERY_9);
        db.execSQL(QUERY_10);
    }

    if (oldVersion < 5){
        db.execSQL(QUERY_11);
    }

    if (oldVersion < 6){
        db.execSQL(QUERY_12);
        db.execSQL(QUERY_13);
    }

提前致谢!

@Override
public void onCreate(SQLiteDatabase db) {

     db.execSQL(CREATE_QUERY);
     db.execSQL(QUERY_2);
     db.execSQL(QUERY_3);
     db.execSQL(QUERY_4);
     db.execSQL(QUERY_5);
     db.execSQL(QUERY_6);
     db.execSQL(QUERY_7);
     db.execSQL(QUERY_8);
     db.execSQL(QUERY_9);
     db.execSQL(QUERY_10);
     db.execSQL(QUERY_11);
     db.execSQL(QUERY_12);
     db.execSQL(QUERY_13);

}

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

    if (oldVersion < 3) {
        db.execSQL(QUERY_2);
        db.execSQL(QUERY_3);
        db.execSQL(QUERY_4);
        db.execSQL(QUERY_5);
        db.execSQL(QUERY_6);
    }

    if (oldVersion < 4) {
        db.execSQL(QUERY_7);
        db.execSQL(QUERY_8);
        db.execSQL(QUERY_9);
        db.execSQL(QUERY_10);
    }

    if (oldVersion < 5){
        db.execSQL(QUERY_11);
    }

    if (oldVersion < 6){
        db.execSQL(QUERY_12);
        db.execSQL(QUERY_13);
    }