简体   繁体   English

在SQLiteOpenHelper上管理sqlite表onUpgrade

[英]Manage sqlite tables on SQLiteOpenHelper onUpgrade

I have an android app published in Playstore. 我在Playstore中发布了一个android应用。 I need to add some columns to the existing table. 我需要向现有表中添加一些列。 When I drop and recreate the table, all data will be lost. 当我删除并重新创建表时,所有数据都将丢失。

Is there any way to alter the database keeping existing data. 有什么方法可以更改保留现有数据的数据库。

To add columns you can use ALTER TABLE your_table ADD COLUMN column_definition without dropping the tables. 要添加列,您可以使用ALTER TABLE your_table ADD COLUMN column_definition而不删除表。

Please have a read of SQL As Understood By SQLite - ALTER TABLE , as there are restrictions. 由于有限制,请阅读《 SQLite理解SQL-ALTER TABLE》 The link also discusses other kinds of schema changes. 该链接还讨论了其他类型的架构更改。

eg you could have something like :- 例如,您可能会遇到类似:-

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion = 1 && newVersion > 1) {
        String alterSQL = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + NEWCOLUMN + " INTEGER DEFAULT 100";
        db.execSQL(alterSQL);
    }
}

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

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