[英]Updating SQLite database when app shipped with database for releasing new version
I am shipping the database file with our android application. 我正在使用我们的android应用程序运送数据库文件。 For that I followed How to ship an Android application with a database? 为此,我遵循了如何将带有数据库的Android应用程序交付? answered by Danny Remington - OMS . 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: 现在,我必须提供新版本,因为数据库架构已更改,因此首先,我将新的db文件从资产复制到data / data目录,并在SqliteOpenHelper
类的onUpgrade
方法中编写插入语句,如下SqliteOpenHelper
:
//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
但是insert语句android.database.sqlite.SQLiteException: no such table: DB_new.table1
了android.database.sqlite.SQLiteException: no such table: DB_new.table1
异常android.database.sqlite.SQLiteException: no such table: DB_new.table1
How I can achieve this.Please help me on this. 我该如何实现。请对此提供帮助。
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. 在您的sqlite助手中,有一个onUpgrade
方法,当您传递给Super的构造函数( SqliteOpenHelper
)的版本更改时,该方法会触发。 Thus, you need to change this number, so that onUpgrade
fires, then put your code for "re-installing" the database inside this method. 因此,您需要更改此数字,以便触发onUpgrade
,然后将用于“重新安装”数据库的代码放入此方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.