简体   繁体   English

Android Sqlite 不更新预填充的数据库

[英]Android Sqlite not updating prepopulated database

I am using prepopulated sqlite database in my android application.我在我的 android 应用程序中使用预填充的 sqlite 数据库。 Now the problem is when i update data in my database and i increment the databse version onUpgrade() method is called but my database is not updated.现在的问题是,当我更新数据库中的数据并增加数据库版本时,调用了onUpgrade()方法,但我的数据库没有更新。 Any help would be appreciated.任何帮助,将不胜感激。

Here is my DatabaseHelper.java这是我的 DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    private Context mContext;
    private SQLiteDatabase mDatabase;

    public DatabaseHelper(Context context) {
        super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
        this.mContext = context;


        // Copy the Database if no database exists
        if (!DatabaseHandler.checkDataBase(context)) {
            DatabaseHandler.copyDataBase(context, Constants.DB_NAME, Constants.DB_VERSION);
        }
        mDatabase = this.getWritableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public void openDatabase() throws SQLException {
        mDatabase = this.getWritableDatabase();
    }

    public Cursor getAllQuotes() {

        String query = "SELECT * FROM " + Constants.TABLE_QUOTE;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        try {
            openDatabase();
            cursor = db.rawQuery(query, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cursor;
    }


    @Override
    public synchronized void close() {
        if (mDatabase != null)
            mDatabase.close();
        super.close();
    }
}

And Here is my DatabaseHandler.java这是我的DatabaseHandler.java

Now the problem is when i update data in my database and i increment the databse version onUpgrade() method is called but my database is not updated.现在的问题是,当我更新数据库中的数据并增加数据库版本时,调用了 onUpgrade() 方法,但我的数据库没有更新。

Assuming that you mean that you have updated the pre-populated database and thus want that new pre-populated database (which has been placed into the assets folder) to be applied.假设您的意思是您已更新预填充数据库,因此希望应用新的预填充数据库(已放入资产文件夹) Then you need to call the appropriate DatabaseHandler's copyDatabase method.然后需要调用相应的DatabaseHandler 的copyDatabase方法。

It is not recommended to do this in the onUpragde method as the database has already been open/allocated at this stage.不建议在onUpragde方法中执行此操作,因为在此阶段数据库已经打开/分配。

It is recommended to check if there is an upgrade due before the SQLiteOpenHelper sub class's super call does the check.建议在 SQLiteOpenHelper 子类的超级调用进行检查之前检查是否有升级到期。

The DatabaseHandler has a method getVersionFromAssetFile that retrieves the version of the asset database but not the version of the database. DatabaseHandler 有一个方法getVersionFromAssetFile检索资产数据库的版本而不是数据库的版本。

If the version in the asset file, were used to check against the coded database version (DB_VERSION) then as the asset file is protected and cannot be changed within the app, testing the coded database version with asset version would result in a mismatch (upgrade) every time the app is run.如果资产文件中的版本用于检查编码数据库版本 (DB_VERSION),那么由于资产文件受到保护且无法在应用程序内更改,因此使用资产版本测试编码数据库版本将导致不匹配(升级) 每次运行应用程序时。

As such you means of checking the coded version against the database version to detect an upgrade.因此,您可以根据数据库版本检查编码版本以检测升级。

As such you could change the DatabaseHandler class to include :-因此,您可以更改 DatabaseHandler 类以包括:-

/**
 * Get the SQLite user_version from the DB
 * @param context   used to get the database path
 * @param databaseName  the name of the database (assumes database is stored in default location)
 * @return the version number as stored in the Database
 */
public static int getVersionFromDatabaseFile(Context context, String databaseName) {
    InputStream is;
    try {
        is = new FileInputStream(context.getDatabasePath(databaseName));
    } catch (IOException e) {
        return OUCH;
    }
    return getDBVersionFromInputStream(is);
}

You could then amend the testing that determines if the asset should be copied to copy the asset if the database doesn't exist or if the database exists to copy the asset (new) if the database version is lower than the coded version.然后,您可以修改确定是否应复制资产以在数据库不存在时复制资产或如果数据库版本低于编码版本时数据库存在时复制资产(新)的测试。

eg :-例如:-

public DatabaseHelper(Context context) {
    super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
    this.mContext = context;

    // Copy the Database if no database exists
    // **NEW** or copy the Database if the database version is less than the
    // coded version (DB_VERSION)
    if (!DatabaseHandler.checkDataBase(context)) {
        DatabaseHandler.copyDataBase(context, Constants.DB_NAME, Constants.DB_VERSION);
    } else {
        if (DatabaseHandler.getVersionFromDatabaseFile(context,Constants.DB_NAME) < Constants.DB_VERSION) {
            DatabaseHandler.copyDataBase(context,Constants.DB_NAME,Constants.DB_VERSION);
        }
    }
    mDatabase = this.getWritableDatabase();
}
  • Note that the above would copy the asset whenever the coded version (DB_VERSION were increased).请注意,每当编码版本(DB_VERSION 增加)时,上述内容都会复制资产。 More complex handling on upgrades could take into consideration the version of the asset database by utilising the getVersionFromDBInAssetFolder eg更复杂的升级处理可以通过使用getVersionFromDBInAssetFolder来考虑资产数据库的版本,例如

     if (DatabaseHandler.getVersionFromDatabaseFile(context,Constants.DB_NAME) < Constants.DB_VERSION && DatabaseHandler.getVersionFromDBInAssetFolder(context,Constants.DB_NAME) == Constants.DB_VERSION ) { DatabaseHandler.copyDataBase(context,Constants.DB_NAME,Constants.DB_VERSION ); }
    • thus the asset copy would only happen when the version in the asset database were the same as the updated DB_VERSION因此,只有在资产数据库中的版本与更新后的 DB_VERSION 相同时才会发生资产副本
  • Note that the code above is in-principle code, it has not been run, so it may contain some errors.请注意,上面的代码是原理代码,尚未运行,因此可能包含一些错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM