Application Update - Android

I am working on an Android application. My application contains a local database located in the / assests folder of the application. When the user installs the application, he copies the database to use it. Then the user can add elements to it.

My question is: if I release an update for my application, it will clear the data (by deleting the copied database), thereby erasing user changes in the database (because the application will copy the database from the resource folder again)

+2
source share
3 answers

No. If the user updates the application (without deleting it - in this case it will be deleted), and the database already exists, Android will not just delete it (it may contain important information!).

Instead, the onUpgrade method of your SQLiteOpenHelper will be called, and you decide whether you want to clear the data or save it.

0
source

It completely depends on how you copy it. Do you always copy it without checking if it exists? Then yes, it will probably be. You really should have shown us some kind of code, though, of course.

0
source

You must override the onUpgrade method in SQLiteOpenHelper:

  /* * (non-Javadoc) * * @see * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite * .SQLiteDatabase) */ @Override public void onCreate(SQLiteDatabase db) { // CREATE TABLE teas (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT // NULL, brew_time INTEGER); String sql = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT NOT NULL, " + BREW_TIME + " INTEGER" + ");"; db.execSQL(sql); } /* * (non-Javadoc) * * @see * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite * .SQLiteDatabase, int, int) */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } 
0
source

All Articles