简体   繁体   English

无法调用onUpgrade()-Android

[英]onUpgrade() not being invoked - Android

I added a column ( SUM ) to my table. 我在表中添加了一个列( SUM )。 I thought all I needed to do to rewrite/recreate my table was increate my variable for DATABASE_VERSION but that is not working. 我以为重写/重新创建表所需要做的就是为DATABASE_VERSION创建变量,但这不起作用。 Do I need something inside my onUpgrade() method? 我的onUpgrade()方法内是否需要一些东西?

Version variable: 版本变量:

private static final int DATABASE_VERSION = 3; 

Constructor: 构造函数:

public DatabaseHelper(Context context) { 
    super(context, DB_NAME, null, DATABASE_VERSION); 
}

onUpgrade() and onCreate() methods: onUpgrade()和onCreate()方法:

 public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE + " ("
            + RANK + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + SCORE + " LONG,"
            + PERCENTAGE + " INTEGER,"
            + SUM + " LONG"
            + ");");
}

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

LogCat output LogCat输出

01-04 02:00:24.635: E/AndroidRuntime(6224): FATAL EXCEPTION: main
01-04 02:00:24.635: E/AndroidRuntime(6224): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: android.database.sqlite.SQLiteException: no such column: sum (code 1): , while compiling: SELECT sum FROM HighscoresList
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.os.Looper.loop(Looper.java:137)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at java.lang.reflect.Method.invokeNative(Native Method)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at java.lang.reflect.Method.invoke(Method.java:511)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at dalvik.system.NativeStart.main(Native Method)
01-04 02:00:24.635: E/AndroidRuntime(6224): Caused by: android.database.sqlite.SQLiteException: no such column: sum (code 1): , while compiling: SELECT sum FROM HighscoresList
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at com.example.test.DatabaseHelper.check(DatabaseHelper.java:82)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at com.example.test.Results.showResults(Results.java:111)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at com.example.test.Results.onCreate(Results.java:60)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.Activity.performCreate(Activity.java:5104)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-04 02:00:24.635: E/AndroidRuntime(6224):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-04 02:00:24.635: E/AndroidRuntime(6224):     ... 11 more

Your onUpgrade() method is empty: 您的onUpgrade()方法为空:

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

At the most basic level you need to add the code to drop the existing table and call onCreate() , then increment DATABASE_VERSION again. 在最基本的级别上,您需要添加代码以删除现有表并调用onCreate() ,然后再次增加DATABASE_VERSION

private static final int DATABASE_VERSION = 4; 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.exec("DROP TABLE IF EXISTS " + TABLE);
    onCreate(db);
}

Yes, in onUpgrade you need to actually update the table, ie add the column: 是的,在onUpgrade您需要实际更新表,即添加列:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 3) {
        db.execSQL("ALTER TABLE " + TABLE + " ADD COLUMN " + SUM + " LONG");
    }
}

You might also want to go back through and populate valid "sum" values for each row if required/desired. 如果需要/需要,您可能还想返回并为每行填充有效的“总和”值。

You need to recreate your table in onUpgrade : 您需要在onUpgrade重新创建表:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    db.execSQL("DROP TABLE IF EXISTS " + TABLE);
    onCreate(db);
}

or else add the column: 否则添加列:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    db.execSQL("ALTER TABLE " + TABLE + " ADD COLUMN `SUM` LONG");
}

You need to implement the onUpgrade method and perform necessary table changes. 您需要实现onUpgrade方法并执行必要的表更改。

If you need to preserve the existing data (or want to take the "right" approach), you should perform an ALTER TABLE statement to add your new column and possibly identify a default value or update the existing rows with the right value. 如果需要保留现有数据(或希望采用“正确”的方法),则应执行ALTER TABLE语句以添加新列,并可能标识默认值或使用正确的值更新现有行。 Same as "Tomas" stated. 与“ Tomas”相同。

Here is an example: 这是一个例子:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    Log.d(TAG, "Upgrading database from version " + oldVersion + " to version  " + newVersion);

    if (newVersion == 3)
    {
        //Moved IO configuration data into flat file, so removing servicedata table.
        db.execSQL(DB_DROP_SERVICEDATA_V3);
    }
    else if (newVersion == 2)
    {
        db.execSQL(DB_ALTER_MESSAGE_V2);
    }
    else
    {
        db.execSQL("drop table if exists " + TABLE_ACCOUNT);
        db.execSQL("drop table if exists " + TABLE_MESSAGE);
        //Old table just checking.
        db.execSQL("drop table if exists servicedata");

        onCreate(db);
    }
}

Yes, that's what onUpgrade() is for. 是的,这就是onUpgrade()目的。

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

    if (oldVersion < 3) {
        upgradeToVersion3();
    }

    // good pattern is to make these incremental updates to ensure that everyone
    // will end up with the same version no matter what version he/she has now
    if (oldVersion < 4) {
        // upgradeToVersion4()
    }

}

private void upgradeToVersion3() {
    db.execSQL("alter table " + TABLE + " add column SUM LONG");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM