Erro do Sqlite no Android : error code 11: database disk image is malformed


I have a version of the Application in production, where it is allowed to bookmark a message, but this totally random error happens causing database corruption and resulting in application failure.

I know that I perform operations a little risky according to the people at SQLite.

The procedure is as follows:

  • starts the upgrade procedure
  • copies the user database to a temporary file
  • copy the new database to the device
  • attach the old base to the new
  • copy necessary data (such as favorite messages)
  • detach the temporary basis
  • excludes the temporary basis

This procedure is done on thousands of devices, most everything goes well, but it started to become frequent cases like this.

Does anyone know how to minimize this?


java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.database.sqlite.SQLiteDatabaseCorruptException: error code 11: database disk image is malformed
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteDatabaseCorruptException: error code 11: database disk image is malformed
    at android.database.sqlite.SQLiteStatement.native_1x1_long(Native Method)
    at android.database.sqlite.SQLiteStatement.simpleQueryForLong(
    at android.database.DatabaseUtils.longForQuery(
    at android.database.DatabaseUtils.longForQuery(
    ... 11 more


A few months ago, we started getting a lot of reports from users complaining about crashes in one of our apps. During investigation of the issue, it became clear that the error was due to some corruption in the SQLite file. I noticed that this problem also existed from a certain period until then. I also saw that this error occurred especially with newer devices that have more processing cores and higher clocks, and that in older devices that are less powerful and especially with only one core, this error occurred very little or simply did not occur. This made me conclude that the problem was related to competition. That here was linked to SQLite multi-thread access.

After some research and testing I saw the following points:

For each database there must be only one SqliteOpenHelper . As much as it sounds, there are no multiple database connections. A SqliteOpenHelper has only one connection. Ever. Even though the getWritableDatabase and getReadableDatabase methods make it look like there are two different connections, it just doesn't happen: it's the same connection.

This ensures that there are no collisions, concurrent accesses, to the database file.

In the case of the application in question, there were multiple SqliteOpenHelper and multiple Threads. The solution was a general persistence refactoring with the goal of having only one SqliteOpenHelper for each database (in this case there were three). There was thus a strong reduction in the occurrences of the problem, however there were still occurrences.

Finally, the database access methods were synchronized in order to avoid concurrent access to the SqliteOpenHelper . This is a measure that did not affect the application's performance, but which must be analyzed for each case.

Summing up:

  1. Keep only one SqliteOpenHelper per database
  2. If necessary and feasible, synchronize access
Scroll to Top