我有一个带有主ID和一些列的sqlite数据库表。现在我想添加一个与主ID具有相同值的附加列。
my_id integer primary key, ..., sort_order INTEGER
为此,我在SQLite Helper的onUpgrade()中执行以下操作
if (oldVersion < 24) {
database.execSQL("ALTER TABLE " + "my_table" + " ADD COLUMN " + "sort_order" + " INTEGER");
database.execSQL("UPDATE my_table SET sort_order = my_id");
}
这对我和此应用的测试用户都很好。
但是现在我将这个应用程序发布到生产环境中,我收到了大量的崩溃报告
android.database.sqlite.SQLiteException: no such column: my_id(code 1): , while compiling: UPDATE my_table SET sort_order = my_id
知道这里会发生什么吗?我回滚到我的应用程序的上一版本并再次更新,但我无法在我的任何设备上重现此崩溃!
更新:实际代码和Stacktrace
创建表:
DATABASE_CREATE = "create table if not exists favrelations (dummyId integer primary key, _id integer, "
+ "room text not null, sort_order INTEGER);";
升级:
if (oldVersion < 24) {
database.execSQL("ALTER TABLE " + "favrelations" + " ADD COLUMN " + "sort_order" + " INTEGER");
database.execSQL("UPDATE favrelations SET sort_order = dummyId");
}
堆栈跟踪:
java.lang.RuntimeException: Unable to create application de.me.MyApp.MyAppApp: java.lang.IllegalStateException: Could not construct instance of helper class class de.me.MyApp.DbAdapter.DataBaseAdapterManager
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3957)
at android.app.ActivityThread.access$1300(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Could not construct instance of helper class class de.me.MyApp.DbAdapter.DataBaseAdapterManager
at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:222)
at com.j256.ormlite.android.apptools.OpenHelperManager.loadHelper(OpenHelperManager.java:170)
at com.j256.ormlite.android.apptools.OpenHelperManager.getHelper(OpenHelperManager.java:78)
at de.me.MyApp.DbAdapter.DataBaseAdapterManager.get(DataBaseAdapterManager.java:81)
at de.me.MyApp.MyAppApp.onCreate(MyAppApp.java:21)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3954)
... 10 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:220)
... 16 more
Caused by: android.database.sqlite.SQLiteException: no such column: dummyId: , while compiling: UPDATE favrelations SET sort_order = dummyId
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
at de.me.MyApp.DbAdapter.DataBaseAdapterManager.onUpgrade(DataBaseAdapterManager.java:285)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onUpgrade(OrmLiteSqliteOpenHelper.java:240)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:170)
at de.me.MyApp.DbAdapter.DataBaseAdapterManager.<init>(DataBaseAdapterManager.java:88)
... 19 more
更新2
我决定抓住错误,删除表并重新创建它。这会导致用户数据丢失,但至少他们可以再次使用该应用程序。我仍然很想知道是什么原因引起的。