-2

Anybody please help me regarding the correct use of onUpgrade() in sqliteopenhelper class.Thanks in advance.

  • 1
    check this link hope it will help you http://developer.android.com/intl/ru/reference/android/database/sqlite/SQLiteOpenHelper.html#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int) – Pradeep Gupta May 09 '16 at 12:17
  • What is the specific problem you are having with `onUpgrade()`? – laalto May 09 '16 at 12:32
  • Actually I have an home automation application which having many devices which saved in local db. I have a doubt that if any updation made by developer,how it reflects in my local db. If all table replaced, I will lose my all data.Is there anyway to backup it?please help me. – Mrithun T P May 09 '16 at 12:49

2 Answers2

2

when you change your database table element or column value then you must have to update to data to reflect that change. other wise no such table or other kind of error will occur. so after changing in database if your application is already uploaded then you have to change database version and all the all table recreate in onUpdate() method.

0

In Sqliteopenhelper you define your database version. If you change your databaseversion onUpgrade() will executed. It is the counterpart of onCreate()

In your onUpgrade you will drop old tables and generate new ones.

There is a small example:

public class BaseDatabaseHandler extends SQLiteOpenHelper {

    /********************************************************
     * Database
     ********************************************************/
    private static final int DATABASE_VERSION = 4;
    private static final String DATABASE_NAME = "test.db";


    /********************************************************
     * Table names
     ********************************************************/
    protected static final String TABLE_INPUT = "input";


    /********************************************************
     * Column names
     ********************************************************/

    /* Input*/
    protected static final String COLUMN_ID = "id";
    protected static final String COLUMN_NAME = "name";


    public BaseDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        String CREATE_INPUT_TABLE =
                "CREATE TABLE " + TABLE_INPUT + " ("
                    + COLUMN_ID + " LONG PRIMARY KEY, "
                    + COLUMN_NAME + " TEXT, "
                    + ")";


        db.execSQL(CREATE_INPUT_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_INPUT);
        onCreate(db);
    }
}
DTeuchert
  • 523
  • 8
  • 19
  • Dear Dteuchert ,thanks for reply.what about my old table data?will it lost if I update my application?Is there anyway to backup my all data.Please explain. – Mrithun T P May 09 '16 at 12:43
  • Hey, yes this data will be get lost. You can program a function which stores your old data in some way and call this function before `db.execSQL("DROP TABLE IF EXISTS " + TABLE_INPUT);` – DTeuchert May 09 '16 at 12:47