How to use DefaultDatabaseErrorHandler to handle database corruption in Android

Some of the live users of my application experience SQLite database corruption . When we collected the magazine from users, we found the details below:

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 

A corrupt database is a Static database (it preloads records, and we do not do any Insert , Update , Change , Delete , except Select data from it.

This database is in the assets of my application, and I install it on the device and get its object using

 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 the DefaultDatabaseErrorHandler using the onCorruption method of this class, but without getting the proper documentation for the implementation, Can I use this class in my case?

+6
source share
2 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 } }); 
+1
source

The database implementation simply tries to open the database again after calling the handler:

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

So, you need to replace the database file with a working copy.

DefaultDatabaseErrorHandler not suitable for you; you must implement your own DatabaseErrorHandler and pass it to openDatabase() .

0
source

Source: https://habr.com/ru/post/1014409/


All Articles