Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite database and app update

Tags:

I have developed an android app and released version 1.0. The app contains SQLite local database with 5 tables.

Now we planned to release version 2.0 also update the version 1.0 users. In version 2.0 we have included extra two tables with previous 5 table, so 7 tables now.

Now my question is, The version 1.0 users all have some data in the local database, If he update to the version 2.0 the previous data will get lost? If it so. then what is the alternate method?

like image 387
Rajesh Rajaram Avatar asked Jun 03 '13 06:06

Rajesh Rajaram


People also ask

How do you update data on Android?

The situation is when userA go to "view profile" activity and click edit info, another activity that call "updateinfo" will come out. Then after userA update his information by clicking update button. It's successful update and go back to "view profile" activity to see his updated profile.

How do I create a SQLite database update?

In order to access this database, you don't need to establish any kind of connections for it like JDBC, ODBC etc. This example demonstrate about How to use update command in Android sqlite. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Does SQLite update?

Introduction to SQLite UPDATE statement First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.


3 Answers

You should put all changes in your onUpgrade method you can use this code:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "ALTER TABLE " + TABLE_SECRET + " ADD COLUMN " +
     "name_of_column_to_be_added" + " INTEGER";
    db.execSQL(sql);        
}        

this adds a column in your current database. Your database will not lose data. Reminder: onUpgrade will be called when getWriteableDatabase or getReadableDatabase is executed AND the version of your database is different from your older version.

like image 92
codebringer Avatar answered Sep 29 '22 18:09

codebringer


Change your db version and add extra two table creation methods in your onUpgrade(SQLiteDatabase db, int oldVersion, int newVesion) method. Now the the previous data will not get lost.

I hope this will help you.

like image 34
Gunaseelan Avatar answered Sep 29 '22 18:09

Gunaseelan


You can do some on SQLiteOpenHelper#onUpgrade, get some old data. and insert into new table

like image 20
Crossle Song Avatar answered Sep 29 '22 19:09

Crossle Song