gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:33:03 27 4
gpt4 key购买 nike

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

一切都很顺利,直到我从手机上完全卸载应用程序并重新构建它。从那时起,我收到错误消息,我要求的所有表格都不存在......

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

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

我知道无论调用 getWritableDatabase() 都会调用 onUpgrade()。但是我的应用程序在这条车道上崩溃了:

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

关于android - onUpgrade() 在新安装的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com