gpt4 book ai didi

java - onUpgrade() 未被调用 - Android

转载 作者:行者123 更新时间:2023-12-01 06:29:48 28 4
gpt4 key购买 nike

我在表格中添加了一列 (SUM)。我认为重写/重新创建表所需要做的就是为 DATABASE_VERSION 创建变量,但这不起作用。我的 onUpgrade() 方法中需要一些东西吗?

版本变量:

private static final int DATABASE_VERSION = 3; 

构造函数:

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

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 输出

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

最佳答案

是的,在onUpgrade中,您需要实际更新表格,即添加列:

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

如果需要/需要,您可能还想返回并填充每行的有效“总和”值。

关于java - onUpgrade() 未被调用 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14163759/

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