如何使用DefaultDatabaseErrorHandler处理Android中的数据库损坏

时间:2017-01-25 10:10:47

标签: android sqlite

我的应用的部分实时用户遇到 SQLite数据库损坏。当我们从用户收集日志时,我们发现以下详细信息:

E/SQLiteLog(14085): (11) database corruption at line 57189 of [b3bb660af9]
E/SQLiteLog(14085): (11) Invalid page count: nPage: 52, nPageFile: 50
E/SQLiteLog(14085): (11) lockBtree() error, rc: 11, printing out first page (size: 32768) of  DB /data/data/com.app.testpackagename/files//db/statictext_v3.0_DE.db
E/SQLiteLog(14085): (11) Page (1) has been corrupted

E/SQLiteLog(13318): (11) database disk image is malformed
E/DefaultDatabaseErrorHandler(13318): Corruption reported by sqlite on database: /data/data/com.app.testpackagename/files//db/statictext_v3.0_DE.db
E/SQLiteLog(13318): (11) database corruption at line 57189 of [b3bb660af9]
E/SQLiteLog(13318): (11) Invalid page count: nPage: 52, nPageFile: 50
E/SQLiteLog(13318): (11) lockBtree() error, rc: 11, printing out first page (size: 32768) of  DB /data/data/com.app.testpackagename/files//db/statictext_v3.0_DE.db
E/SQLiteLog(13318): (11) Page (1) has been corrupted

损坏的数据库是静态数据库(它已预加载记录,我们没有做任何插入更新更改删除,但选择数据除外。

此数据库位于我的应用程序的资产中,我将其安装到设备并使用

获取其对象
this.staticDb = SQLiteDatabase.openDatabase(AppDelegate.getFileDirectory() + "/" + SDCARD_FOLDER_NAME +DB_FOLDER_NAME + "/" + Dbpath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);

我想使用DefaultDatabaseErrorHandler使用此类的onCorruption方法来处理此异常,但是没有获得任何适当的实现文档。我可以在我的案例中使用这个课吗?

2 个答案:

答案 0 :(得分:1)

SQLiteDatabase.openDatabase(context.getFileDirectory() + "/" + "sd_card_name" + "db_folder_name" + "/" + "dbPath",
                null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY,
                new DatabaseErrorHandler() {
                    @Override
                    public void onCorruption(SQLiteDatabase dbObj) {
                        //do whatever you want to do
                    }
                });

答案 1 :(得分:0)

database implementation只是在调用处理程序后再次尝试打开数据库:

private void open() {
    ...
        try {
            openInner();
        } catch (SQLiteDatabaseCorruptException ex) {
            onCorruption();
            openInner();
        }
    ...

所以你要做的就是用工作副本替换数据库文件。

DefaultDatabaseErrorHandler对你没用;您必须实施自己的DatabaseErrorHandler并将其提交给openDatabase()