-1

I am working on an android application that is using SQLite database to store data. I have "Favorite" table in database for storing favorite items of particular user.

The problem is when user will get an update of this application then I want to update database except "Favorite" table, because it will contain user specific favorite items.

Edit:

Now I know that I can update records on some tables in onUpgrade() event of SQLite database, but I really don't know how to manage two databases in an application? I have searched on Google but not get any idea to do this, there are many example to copy data from asset folder to new database.

I have to select all records from a table from "existed database" and copy to table of "new database". Table name may be same of both databases.

Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54

2 Answers2

2

There are 2 aspects of updating Android app and DB:
1) If you just upgrade your app and don't change your DATABASE_VERSION (last parameter when you implement constructor of your class-descendant from SQLiteOpenHelper) - old database is retained and you may use it without any additional actions

2) If you change your DATABASE_VERSION- in your implementation of SQLiteOpenHelper there is method onUpgrade() which may look like this:

@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // RUN YOUR SQL SCRIPTS:
            db.execSQL(DBContract.Table1.SQL_DROP_TABLE);
            ...
            // JUST DON'T DROP YOU FAVORITE TABLE HERE

            // if required call onCreate():
            onCreate(db);
        }

So it's up to you whether to drop table or use it further.
More than that - you may decide what to do on the value of oldVersion and newVersion

Edit If (according to your comments) you want to move data between databases you may look at this post

Community
  • 1
  • 1
sberezin
  • 3,266
  • 23
  • 28
  • Thanks for replying. I got the answer, I can preserve a table. Please suggest how can I insert bulk of data to existing database, because there may be thousands of record in other tables, so how can i update the tables? client may give the new data in sqlite database, or in CSV file. – Ashish Tiwari Jan 23 '15 at 06:31
  • Is that 'bulk of data' from the same database? Then you may handle it in onUpgrade(). If you need to process data - create temporary tables, manipulate data and drop them after you've finished – sberezin Jan 23 '15 at 06:38
  • Yes it is bulk of data from the same database. Can I keep new database in asset folder and from there can I fetch the data from new db and put them on new db except "favorite" table? – Ashish Tiwari Jan 23 '15 at 06:44
  • Please see edit in the post. But I'm not sure I've understood your question fully. If I don't answer your question - please edit/clarify your post – sberezin Jan 23 '15 at 07:07
0

If you have copied your DB in app data or sd card thn it will remain even after updating data.*** if user is not clearing data manually from app settings

  • If no problem to download data from internet thn you by creating API download data and update the data in required tables
  • Other if you dont wants to go with above one .. than create data file for each table which you wants to update, keep it in assets folder and after updating app when user starts app first time you can update data except that fav table.eg Table wise json file with new data which you wants to update.

Hope I understand you question. and if not thn pls share it will try on tht

user1140237
  • 5,015
  • 1
  • 28
  • 56