SQLite添加列,保留数据

时间:2011-10-01 18:26:49

标签: android sqlite

我有一个包含4列的数据库:

@Override
public void onCreate(SQLiteDatabase database) {
        database.execSQL("CREATE TABLE " + DATABASENAME + " (name TEXT, latitude REAL, longitude REAL, country TEXT);");
}

现在我想添加另一列,但保留已存在于数据库中的行。我该怎么做?我找不到任何有用的参考资料。

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
    //What to do here?
}

修改

我已将onCreate修改为:

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL("CREATE TABLE " + DATABASENAME + " (name TEXT, latitude REAL, longitude REAL, country TEXT, code TEXT);");
}

和我的onUpgrade:

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
    Logger.log("Updating userstations database from " + arg1 + " to " + arg2 + ".");
    if (arg1 == 2 && arg2 == 3) {
        db.execSQL("ALTER TABLE " + DATABASENAME + " ADD COLUMN code TEXT;");
    } else if(arg1 == 1){
        db.execSQL("DROP TABLE IF EXISTS " + DATABASENAME);
    }
    onCreate(db);
}

但是,现在我得到了这个堆栈跟踪:

10-01 21:24:19.581: ERROR/Database(21434): Failure 1 (table userstations already exists) on 0x19c378 when preparing 'CREATE TABLE userstations (name TEXT, latitude REAL, longitude REAL, country TEXT, code TEXT);'.
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434): Couldn't open userstations for writing (will try read-only):
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434): android.database.sqlite.SQLiteException: table userstations already exists: CREATE TABLE userstations (name TEXT, latitude REAL, longitude REAL, country TEXT, code TEXT);
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.databases.UserStationsOpenHelper.onCreate(UserStationsOpenHelper.java:25)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.databases.UserStationsOpenHelper.onUpgrade(UserStationsOpenHelper.java:36)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.databases.UserStationsOpenHelper.getStations(UserStationsOpenHelper.java:43)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.Data.readUserStations(Data.java:369)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.Data.getUserStations(Data.java:210)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.stationstab.StationsActivity.newStations(StationsActivity.java:190)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.stationstab.StationsActivity.refresh(StationsActivity.java:129)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.stationstab.StationsActivity.onCreate(StationsActivity.java:108)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1598)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.stationstab.StationsActivityGroup.onCreate(StationsActivityGroup.java:38)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1598)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.widget.TabHost.addTab(TabHost.java:216)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.TreinVerkeer.setupTab(TreinVerkeer.java:131)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.TreinVerkeer.initTabs(TreinVerkeer.java:108)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.myapp.myapp.TreinVerkeer.onCreate(TreinVerkeer.java:62)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.os.Looper.loop(Looper.java:123)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at android.app.ActivityThread.main(ActivityThread.java:3835)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at java.lang.reflect.Method.invoke(Method.java:507)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-01 21:24:19.611: ERROR/SQLiteOpenHelper(21434):     at dalvik.system.NativeStart.main(Native Method)
10-01 21:24:19.621: DEBUG/AndroidRuntime(21434): Shutting down VM
10-01 21:24:19.621: WARN/dalvikvm(21434): threadid=1: thread exiting with uncaught exception (group=0x4018a560)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434): FATAL EXCEPTION: main
10-01 21:24:19.641: ERROR/AndroidRuntime(21434): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.TreinVerkeer}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.stationstab.StationsActivityGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.stationstab.StationsActivity}: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 2 to 3: /data/data/com.myapp.myapp/databases/userstations
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.os.Looper.loop(Looper.java:123)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.main(ActivityThread.java:3835)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at java.lang.reflect.Method.invoke(Method.java:507)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at dalvik.system.NativeStart.main(Native Method)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.stationstab.StationsActivityGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.stationstab.StationsActivity}: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 2 to 3: /data/data/com.myapp.myapp/databases/userstations
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1598)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.widget.TabHost.addTab(TabHost.java:216)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.myapp.myapp.TreinVerkeer.setupTab(TreinVerkeer.java:131)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.myapp.myapp.TreinVerkeer.initTabs(TreinVerkeer.java:108)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.myapp.myapp.TreinVerkeer.onCreate(TreinVerkeer.java:62)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     ... 11 more
10-01 21:24:19.641: ERROR/AndroidRuntime(21434): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.stationstab.StationsActivity}: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 2 to 3: /data/data/com.myapp.myapp/databases/userstations
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1598)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.myapp.myapp.stationstab.StationsActivityGroup.onCreate(StationsActivityGroup.java:38)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     ... 22 more
10-01 21:24:19.641: ERROR/AndroidRuntime(21434): Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 2 to 3: /data/data/com.myapp.myapp/databases/userstations
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:199)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.myapp.myapp.databases.UserStationsOpenHelper.getStations(UserStationsOpenHelper.java:43)
10-01 21:24:19.641: ERROR/AndroidRuntime(21434):     at com.myapp.myapp

4 个答案:

答案 0 :(得分:48)

有关在表格上创建新列的语法,请参阅this页面。基本上是:

ALTER TABLE mytable ADD COLUMN mycolumn TEXT

在你的onUpgrade方法中,它看起来像这样:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String upgradeQuery = "ALTER TABLE mytable ADD COLUMN mycolumn TEXT";
    if (oldVersion == 1 && newVersion == 2)
         db.execSQL(upgradeQuery);
}

答案 1 :(得分:6)

更好的方法

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    switch (oldVersion) {
        case 1:
            db.execSQL(SQL_MY_TABLE);


        case 2:
            db.execSQL("ALTER TABLE myTable ADD COLUMN myNewColumn TEXT");

    }
}

让我们说在案例1中,你升级到数据库版本2.你创建了一个新表,但忘记了你将在案例2中看到的myNewColumn。如果你将db版本更改为3,那么案例2将会得到如果从2升级到3,则运行。

答案 2 :(得分:2)

我没有使用过android,但sqlite提供了'alter table',就像大多数SQL实现一样:

SQLite alter table

答案 3 :(得分:0)

例如在版本2中,向数据库添加新列的正确方法是:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
         db.execSQL("ALTER TABLE mytable ADD COLUMN mycolumn TEXT");
    }
}

它涵盖了所有陷阱,包括所选答案的主要问题:如果用户从版本1升级到版本3,他们将完全错过升级查询!这些用户将处于尴尬的境地,他们缺少一些中间更新,并且没有预期的sql模式。

也不要忘记更改create语句以添加新列。