如何使用已更改COLUMN名称的新数据库迁移现有的Sqlite数据库? Android的

时间:2017-06-29 11:47:36

标签: android sqlite android-sqlite sqliteopenhelper

嗨我在Play商店有一个记事本应用程序并且有用户数据...没有这些新更改的应用程序可以更新,导出和放大导入备份并完美运行没有问题。但是当我改变旧的SQLiteOpenHelper安排时,现在它并没有做我上面提到的任何事情,除非我改变旧安排的新改变。

旧的SQLiteOpenHelper安排

    static final String DATABASE_NAME = "note.db";
    static final String TABLE_NOTE = "tb_note";
    static final String KEY_ID_NOTE = "id";
    private static final String KEY_TITLE_NOTE = "title";
    private static final String KEY_CONTENT_NOTE = "content";
    private static final String KEY_LAST_MODIFIED_NOTE = "last_modified";


    private static final String CREATE_TABLE_NOTE =
            "CREATE TABLE " + TABLE_NOTE + "(" +
                    KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
                    ", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
                    "," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
                    "," + KEY_LAST_MODIFIED_NOTE + " TEXT DEFAULT \'\'" +
                    ")";


    private static final int DATA_VERSION = 2;

但是我在应用程序中更改了一个COLUMN名称,而我的新SQLiteOpenHelper变得像这样,除了我在这段代码下面用粗体更改了这些行之外,一切都是一样的

    static final String DATABASE_NAME = "note.db";
    static final String TABLE_NOTE = "tb_note";
    static final String KEY_ID_NOTE = "id";
    private static final String KEY_TITLE_NOTE = "title";
    private static final String KEY_CONTENT_NOTE = "content";
    private static final String KEY_UPDATE_TIME = "update_time";


    private static final String CREATE_TABLE_NOTE =
            "CREATE TABLE " + TABLE_NOTE + "(" +
                    KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
                    ", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
                    "," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
                    "," + KEY_UPDATE_TIME + " TEXT NOT NULL" +
                    ")";


    private static final int DATA_VERSION = 2;

他们就像这样

private static final String KEY_LAST_MODIFIED_NOTE =" last_modified";

"" + KEY_LAST_MODIFIED_NOTE +"文字默认\' \'" +

但现在

私有静态最终字符串KEY_UPDATE_TIME =" update_time";

"" + KEY_UPDATE_TIME +" TEXT NOT NULL" +

最后这是我的升级代码

 @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // update database for database version < 2
        if (oldVersion < 2) {
            db.execSQL("ALTER TABLE " + TABLE_NOTE + " ADD COLUMN " + KEY_UPDATE_TIME + " TEXT DEFAULT \'\'");
        }
    }

现在如何使用新的数据库迁移这个现有的Sqlite数据库,而不会丢失任何数据?

1 个答案:

答案 0 :(得分:0)

首先,重命名旧表,然后使用旧表名和更新列名创建一个新表,然后将第一个表内容复制到新创建的表中并删除旧表。

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       // change old table name 
        db.execSQL("ALTER TABLE "+orig_table_name +" RENAME "+ " TO "+ tmp_table_name;
     // create new table with old table name and updated coulmn
         db.execSQL( "CREATE TABLE " + TABLE_NOTE + "(" +
                    KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
                    ", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
                    "," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
                    "," + KEY_UPDATE_TIME + " TEXT NOT NULL" +
                    ")");
// then insert old table data into new one
db.execSQL(""INSERT INTO " + NEW_TABLE NAME + " SELECT * FROM " + OLD_TABLE_NAME");
//and finally delete old table
 db.execSQL("DROP TABLE "+OLD_TABLE_NAME); 
    }

使用sqlite浏览器检查上述所有查询是否成功执行。