Reputation: 87
I am shipping the database file with our android application. For that I followed How to ship an Android application with a database? answered by Danny Remington - OMS.
Now I have to provide the new release In that the database schema has changed, so First I am copying the new db file from assets to data/data directory and writing a insert statement as following in the onUpgrade
method of SqliteOpenHelper
class:
//rename the old database
new File(DB_PATH + DATABASE_NAME).renameTo(new File(DB_PATH + "DB_old"));
try
{
//copting new db file from assets to data/data dir
copyDataBase();
}
catch (IOException e)
{
e.printStackTrace();
}
SQLiteDatabase DB_old = SQLiteDatabase.openDatabase(DB_PATH + "DB_old", null, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase DB_new = SQLiteDatabase.openDatabase(DB_PATH + "DB_new", null, SQLiteDatabase.OPEN_READWRITE);
//insert statement from old to new db
String query = "INSERT INTO DB_new.table1(user_id,user_name) SELECT * FROM DB_old.table1";
DB_new.execSQL(query);
DB_old.close();
DB_new.close();
//deleting old db file
new File(DB_PATH + "DB_old").delete();
But the insert statement is throwing a exception android.database.sqlite.SQLiteException: no such table: DB_new.table1
How I can achieve this.Please help me on this.
Upvotes: 1
Views: 189
Reputation: 6109
In your sqlite helper, there is a method onUpgrade
which triggers when the version you pass to the super's constructor (of the SqliteOpenHelper
) changes. Thus, you need to change this number, so that onUpgrade
fires, then put your code for "re-installing" the database inside this method.
Upvotes: 1