简体   繁体   English

Android SQLiteAssetHelper无法将只读数据库从版本1升级到2:

[英]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 your SQLiteAsstHelper 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 在这种情况下,遵循以下方法

  1. rename existing table (say table_xyz) to be altered to some temp name (say temp_table_xyz) 重命名现有表(比如table_xyz)以更改为某个临时名称(比如temp_table_xyz)
  2. create the table (table_xyz) with new schema 使用新架构创建表(table_xyz)
  3. copy the contents from temp table temp_table_xyz to the new table_xyz 将临时表temp_table_xyz中的内容复制到新的table_xyz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android:无法将只读数据库从版本0升级到1 - android: Can't upgrade read-only database from version 0 to 1 SQLiteException:无法将只读数据库从版本1升级到2 - SQLiteException: Can't upgrade read-only database from version 1 to 2 android.database.sqlite.SQLiteException:无法将只读数据库从版本0升级到版本1 - android.database.sqlite.SQLiteException : Can't upgrade read-only database from version 0 to 1 引起:android.database.sqlite.SQLiteException:无法将只读数据库从版本0升级到1 - Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1 原因:android.database.sqlite.SQLiteException:无法从版本升级只读数据库 - Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version Android Map App,无法将只读数据库从版本0升级到版本1 - Android map app, can't upgrade read-only database from version 0 to 1 SQLiteException:无法将只读数据库从版本X升级到Y - SQLiteException: Can't upgrade read-only database from version X to Y SQLiteAssetHelper:无法打开数据库进行写入(将尝试只读) - SQLiteAssetHelper:Couldn't open database for writing (will try read-only) SQLITE无法将只读数据库从版本1升级到版本2 - SQLITE cant upgrade read-only database from version 1 to 2 升级只读数据库 - Upgrade read-only database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM