FileDocCategorySizeDatePackage
DefaultDatabaseErrorHandler.javaAPI DocAndroid 5.1 API4421Thu Mar 12 22:22:10 GMT 2015android.database

DefaultDatabaseErrorHandler

public final class DefaultDatabaseErrorHandler extends Object implements DatabaseErrorHandler
Default class used to define the actions to take when the database corruption is reported by sqlite.

An application can specify an implementation of {@link DatabaseErrorHandler} on the following:

  • {@link SQLiteDatabase#openOrCreateDatabase(String, android.database.sqlite.SQLiteDatabase.CursorFactory, DatabaseErrorHandler)}
  • {@link SQLiteDatabase#openDatabase(String, android.database.sqlite.SQLiteDatabase.CursorFactory, int, DatabaseErrorHandler)}
The specified {@link DatabaseErrorHandler} is used to handle database corruption errors, if they occur.

If null is specified for DatabaeErrorHandler param in the above calls, then this class is used as the default {@link DatabaseErrorHandler}.

Fields Summary
private static final String
TAG
Constructors Summary
Methods Summary
private voiddeleteDatabaseFile(java.lang.String fileName)

        if (fileName.equalsIgnoreCase(":memory:") || fileName.trim().length() == 0) {
            return;
        }
        Log.e(TAG, "deleting the database file: " + fileName);
        try {
            SQLiteDatabase.deleteDatabase(new File(fileName));
        } catch (Exception e) {
            /* print warning and ignore exception */
            Log.w(TAG, "delete failed: " + e.getMessage());
        }
    
public voidonCorruption(android.database.sqlite.SQLiteDatabase dbObj)
defines the default method to be invoked when database corruption is detected.

param
dbObj the {@link SQLiteDatabase} object representing the database on which corruption is detected.


                                   
        
        Log.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());

        // is the corruption detected even before database could be 'opened'?
        if (!dbObj.isOpen()) {
            // database files are not even openable. delete this database file.
            // NOTE if the database has attached databases, then any of them could be corrupt.
            // and not deleting all of them could cause corrupted database file to remain and 
            // make the application crash on database open operation. To avoid this problem,
            // the application should provide its own {@link DatabaseErrorHandler} impl class
            // to delete ALL files of the database (including the attached databases).
            deleteDatabaseFile(dbObj.getPath());
            return;
        }

        List<Pair<String, String>> attachedDbs = null;
        try {
            // Close the database, which will cause subsequent operations to fail.
            // before that, get the attached database list first.
            try {
                attachedDbs = dbObj.getAttachedDbs();
            } catch (SQLiteException e) {
                /* ignore */
            }
            try {
                dbObj.close();
            } catch (SQLiteException e) {
                /* ignore */
            }
        } finally {
            // Delete all files of this corrupt database and/or attached databases
            if (attachedDbs != null) {
                for (Pair<String, String> p : attachedDbs) {
                    deleteDatabaseFile(p.second);
                }
            } else {
                // attachedDbs = null is possible when the database is so corrupt that even
                // "PRAGMA database_list;" also fails. delete the main database file
                deleteDatabaseFile(dbObj.getPath());
            }
        }