[英]Android SQLiteAssetHelper Can't upgrade read-only database from version 1 to 2:
I want to create an offline dictionary app, that need sometimes to get update and old database will be replaced by new one. 我想创建一个离线词典应用程序,有时需要更新,旧数据库将被新的替换。 It's what I want to do, and I did something like this with
SQLiteAssetHelper
Library: 这就是我想做的事情,我使用
SQLiteAssetHelper
Library做了类似的事情:
Note: SQLiteAssetHelper
will copy database from assets folder into app data folder 注意:
SQLiteAssetHelper
会将数据库从assets文件夹复制到app data文件夹中
public class MyDb extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "db.sqlite";
private static final int DATABASE_VERSION = 1;
public MyDb(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
now i want to update database, after puting new db.sqlite
file into assets
folder, I have manipulate my codes like this: 现在我想更新数据库,在将新的
db.sqlite
文件放入assets
文件夹后,我操作了我的代码,如下所示:
public class MyDb extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "db.sqlite";
private static final int DATABASE_VERSION = 2;
public MyDb(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
but when i compiled and run, it says: Can't upgrade read-only database from version 1 to 2 但是当我编译并运行时,它说: 无法将只读数据库从版本1升级到2
what is the solution? 解决办法是什么?
by clearing app data, it will works fine... 通过清除应用数据,它将正常工作......
SORRY FOR MY BAD ENGLISH... 对不起,我的英语不好...
Quoting the documentation : 引用文档 :
If you have a read-only database or do not care about user data loss, you can force users onto the latest version of the SQLite database each time the version number is incremented (overwriting the local database with the one in the assets) by calling the
setForcedUpgrade()
method in yourSQLiteAsstHelper
subclass constructor.如果您有一个只读数据库或者不关心用户数据丢失,那么每次通过调用增加版本号(用资产中的一个覆盖本地数据库)时,您可以强制用户访问最新版本的SQLite数据库
SQLiteAsstHelper
子类构造函数中的setForcedUpgrade()
方法。You can additionally pass an argument that is the version number below which the upgrade will be forced.
您还可以传递一个参数,该参数是强制升级的版本号。
Note that this will overwrite an existing local database and all data within it.
请注意,这将覆盖现有的本地数据库及其中的所有数据。
You are not calling setForcedUpgrade()
from your constructor. 您没有从构造函数中调用
setForcedUpgrade()
。
You should call setForcedUpgrade();
你应该调用
setForcedUpgrade();
after your Constructor : 在你的构造函数之后 :
public class MyDb extends SQLiteAssetHelper {
private static final int DATABASE_VERSION = 2;//+1
public MyDb(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
setForcedUpgrade();
}
}
It's solved by this: 它解决了这个问题:
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
context.deleteDatabase(DATABASE_NAME);
new MyDb(context);
}else
super.onUpgrade(db, oldVersion, newVersion);
}
It's very useful for some database that we don't need old data when getting update. 对于某些数据库而言,它在获取更新时不需要旧数据非常有用。
Please note that deleting the old db and creating new one in onUpgrade is not the right solution. 请注意,删除旧数据库并在onUpgrade中创建新数据库不是正确的解决方案。 Older data has to be migrated.
必须迁移旧数据。 Incase you are not interested in maintaining older data ov version 1, it is alright.
如果您对维护旧版数据ov版本不感兴趣,那就没关系。
Right way is to use onUpgrade to modify the schema. 正确的方法是使用onUpgrade来修改架构。 Most of the time altering the schema required recreating table.
大多数时候改变模式需要重新创建表。 In such cases follow below approach
在这种情况下,遵循以下方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.