Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DefaultDatabaseErrorHandler to handle database corruption in Android

Tags:

android

sqlite

Some of the live users of my app are experiencing SQLite database corruption. When we have collected log from users we found below details:

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

The corrupt database is the Static database (it has preloaded records and we are not doing any Insert, Update, Alter, Delete except Select data from it.

This database is in assets of my app and I'm mounting it to device and obtaining its object with

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

I want to handle this exception with DefaultDatabaseErrorHandler by using onCorruption method of this class but not getting any proper documentation for implementation. Can I use this class in my case?

like image 772
Gopal Avatar asked Jan 25 '17 10:01

Gopal


Video Answer


1 Answers

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
                    }
                });
like image 167
Dibzmania Avatar answered Nov 03 '22 00:11

Dibzmania