简体   繁体   中英

Steps to upgrade existing Android database without losing exisiting data

I have been trying this for two days and have looked at all the different post on it.

I followed this post as well.

Steps to upgrade existing Android database w/o losing data

But when I install a new version on my phone it seems to wipe out my old data.

Can some one please helo.

Here is my database code

private static final int DATABASE_VERSION = 2; //was 1 before
public void create() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
        this.getWritableDatabase();
    }else{

        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.


        try {
            this.getReadableDatabase();
            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(newVersion == 2) {
        db.execSQL("ALTER TABLE CHANNELPROFILE ADD COLUMN backcolor TEXT");
    }
}

Not really sure what I am doing wrong here. please help

Try this:

String TABLE_NAME= "train_day";

String column1= "column1";
String column2= "column2";
String column3= "column3";

String TABLE_BODY = TABLE_NAME + " ("
            + column1 + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + column2 + " TEXT NOT NULL,"
            + column3 + " TEXT NOT NULL)";

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    replaceDataToNewTable(db, TABLE_NAME, TABLE_BODY);

}

private void replaceDataToNewTable(SQLiteDatabase db, String tableName, String tableString){
    db.execSQL("CREATE TABLE IF NOT EXISTS " + tableString);

    List<String> columns = getColumns(db, tableName);
    db.execSQL("ALTER TABLE " + tableName + " RENAME TO temp_" + tableName);
    db.execSQL("CREATE TABLE " + tableString);

    columns.retainAll(getColumns(db, tableName));
    String cols = join(columns, ",");
    db.execSQL(String.format("INSERT INTO %s (%s) SELECT %s from temp_%s",
            tableName, cols, cols, tableName));
    db.execSQL("DROP TABLE temp_" + tableName);
}

private List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> ar = null;
    Cursor c = null;
    try {
        c = db.rawQuery("select * from " + tableName + " limit 1", null);
        if (c != null) {
            ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
        }
    } catch (Exception e) {
        LOGE(tableName, e.getMessage() + e);
        e.printStackTrace();
    } finally {
        if (c != null)
            c.close();
    }
    return ar;
}

private String join(List<String> list, String divider) {
    StringBuilder buf = new StringBuilder();
    int num = list.size();
    for (int i = 0; i < num; i++) {
        if (i != 0)
            buf.append(divider);
        buf.append(list.get(i));
    }
    return buf.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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