-2

I have read many questions & answers related to my question but I am not getting a proper way for doing this.

Condtion:

I have three tables in SQLite database. I want to update the data of two (TBL_NAMES,TBL_CATEGORY) tables without losing the data of third table (TBL_FEVORITE) whenever my application get an update.

My application would work offline, so the new records will be provided to me with SQLite database.

What I did?

  • I did put the updated database in asset folder and tried to access from my `DatabaseHelper' class but, it is not accessible.

  • I tried to create two different database (one for TBL_NAMES & TBL_CATEGORY and second one for TBL_FEVORITE) but did not succeeded.

Please suggest how can I do this? How can I keep two database open if I would use two databases or how can I update the database from updated database except one table.

Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
  • When you say "_update the data of two ... tables_", do you mean you want to *merge* (in some way) the old and new information in those tables, or *replace* the old versions with what's in the new update? – TripeHound Jan 27 '15 at 13:02
  • When I say update the two tables then it means I want to replace the old versions with what's in the new update . – Ashish Tiwari Jan 27 '15 at 13:05
  • @AshishTiwari do you mean migrate the database? What do you mean with "update" and how are the new records provided? – sockeqwe Jan 27 '15 at 13:11
  • The new record will be provided in a SQLite database. Meaning of update: "suppose currently I have 1000 names in my table, and I deploy the app on google play store. After some times new names are provided in other sqlite DB to add in the application then I have to replace the old record with new one. I dont want to loss data of fevorite because this tables will contain user specific or user's favorite name" – Ashish Tiwari Jan 27 '15 at 13:17
  • I have updated my answer (see below) – sockeqwe Jan 27 '15 at 13:50

1 Answers1

2

Im not sure if i understand your question, but you can't use SQL JOIN on two (or more) databases. You can only JOIN Tables of the same database. If you really want to JOIN databases, then you have to do that by hand i.e. query a list of records from database1.tableA , query list of records from database2.tableB, then save them in a hashmap with the join condition as key and join them and save the join result

Update: Ok, so if you want to add records from "newDatabase" (provided to you) to the "usersDatatbase" (the current database of the user of your app) with an app update you simply have to versioning the database. Every database in android has a version and you can react on version changes in SQLiteOpenHelper onUpgrade(). Usually you would use this method to alter the table schema, but I think it's completely ok for what you are going to do. So let's assume you have released you app to the playstore with final int DATABASE_VERSION = 1. After some weeks you decide to update the database. So what you have to do is set final int DATABASE_VERSION = 2 (sets database version to 2) and release the new update to the playstore. If the user of your app installs your update from playstore (with database version == 2) onUpgrade() will be called, where you check the old version of the database and the new version and do the data migration (i.e. rename the names with the new provided ones). After a month you want to update the database to final int DATABASE_VERSION = 3 and so on. I hope you get the point. The only thing you have to keep in mind is, that a user can skip an playstore update of your app. For instance a user with app version 1 have not updated to version 2 but directly upgrades from version 1 to 3. In that case you may (depends on your database changes you want to apply) have to migrate from the database from 1 to 2 and then from 2 to 3.

Example:

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_COMMENTS = "comments";   public static final String COLUMN_ID = "_id";   public static final String COLUMN_COMMENT = "comment";

  private static final String DATABASE_NAME = "commments.db";   private static final int DATABASE_VERSION = 3;

  // Database creation sql statement   private static final String DATABASE_CREATE = "create table "
      + TABLE_COMMENTS + "(" + COLUMN_ID
      + " integer primary key autoincrement, " + COLUMN_COMMENT
      + " text not null);";

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

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

     if (newVersion == 2){
        migrateFrom1To2(db);
     }

     if (newVersion == 3){
        if (oldVersion == 1){
           migrateFrom1To2(db);
           migrateFrom2To3(db);
        } else {  
          // oldversion == 2
          migrateFrom2To3(db);
        }
     }
   }

 private void migrateFrom1to2(SQLiteDatabase db){
    // TODO insert the new data into the users local database
 }

 private void migrateFrom2To3(SQLiteDatabase db){
    // TODO insert the new data into the users local database
 }

}
sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • I think this question is to do with the way Android apps using SQLite get updated ... I'm not an expert (my comment above was mainly to elicit clarification for others) but when an app is updated, code can be called to replace an existing database; I'm guessing the OP is trying to keep _some_ of the old data when this happens. – TripeHound Jan 27 '15 at 13:06
  • @sockeqwe Thanks for answering.Your answer gives me clear idea but one thing is still not clear. Please suggest about data migration. Suppose I have to deploy second version of the app and I have latest database then how can I replace/copy the data of two table in "usersDatatbase" (the current database of the user of your app). New data will be provided me in a SQLite database. – Ashish Tiwari Jan 28 '15 at 04:26
  • @sockeqwe: Did you get my problem? – Ashish Tiwari Jan 28 '15 at 13:06
  • 1
    @AshishTiwari I don't see a problem, you can simply open the Database of version2 (i.e. from assets folder http://stackoverflow.com/questions/2605555/android-accessing-assets-folder-sqlite-database-file-with-sqlite-extension ) in `migrateFrom1to2()` and query that data from version 2 database and insert / update the users local database. You could also consider instead of using putting the whole Database (Version 2) in the assets folder to put a text file which contains the right SQL Statements to execute to migrate the local users database, but that depends on your data ... – sockeqwe Jan 28 '15 at 14:41