2017-06-29 77 views
-1

您好我對Play商店中的記事本應用,並得到了用戶的數據......如果沒有這些新的變化,應用程序可以更新,出口&導入備份和沒有問題的完美運行。但是當我更改舊的SQLiteOpenHelper安排時,現在它不會執行上面提到的任何操作,除非我將這些新更改恢復爲舊安排。如何遷移已更改COLUMN名稱的新Sqlite數據庫? Android的

老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; 

但我的應用改變了一組的名字和我的新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; 

他們這樣

普里瓦e static final String KEY_LAST_MODIFIED_NOTE =「last_modified」;

「」 + KEY_LAST_MODIFIED_NOTE + 「TEXT DEFAULT \ '\'」 +

但現在

私有靜態最後絃樂KEY_UPDATE_TIME = 「UPDATE_TIME」;

「」 + KEY_UPDATE_TIME + 「TEXT NOT NULL」 +

終於在這裏是我的onupgrade代碼

@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 \'\'"); 
     } 
    } 

現在如何這個exsiting SQLite數據庫用新的遷移,而不會丟失任何數據?

回答

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瀏覽器檢查所有以上查詢成功執行。

+0

謝謝@Anil你能寫出來,看看它是如何工作...我真的很感激我的應用程序依賴於它:) – Khamkhor