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.
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
}
}