16

In my android app I am getting "database disk image is malformed" What are the reasons for getting this error?

not closed db? multiple threads accessing the db? or android just got corrupted?

Thanks

android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
   android.database.sqlite.SQLiteQuery.native_fill_window(Native Method)
   android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:75)
   android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:288)
   android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:269)
   android.database.AbstractCursor.moveToPosition(AbstractCursor.java:171)
   android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)
...
Natali
  • 2,934
  • 4
  • 39
  • 53
Daniel Benedykt
  • 6,496
  • 12
  • 51
  • 73
  • This solution I found out might help you. [Fix to SQLite database malformed error in Android Studio](https://stackoverflow.com/a/69074969/11603040) – Kingsley Onwus Sep 06 '21 at 13:10

2 Answers2

4

I've come across many instances of people reporting sqlite corruption issues on Android. The majority of times it's related to a task killer but there is still a small margin of people still experiencing random SQLite corruption.

For example, this issue: http://code.google.com/p/android/issues/detail?id=4866

JRL's links above are also very useful for understanding what types of events can lead to a corrupted SQLite DB image.

Also, SQLite itself has been patched in recent history to resolve various (rare) data corruption scenarios. See http://www.sqlite.org/changes.html. So the version of SQLite that ships with Android isn't the latest, but as Android evolves, so do the apps that are bundled with it, such as SQLite.

At the end of the day, there's only so much we can do as programmers to safeguard against SQLite corruption so its sometimes advantageous to code safety mechanisms into our apps, such as periodic DB backups to SDCard (that's what I do).

Brad Hein
  • 10,997
  • 12
  • 51
  • 74
  • How do you take periodic db backups ? copy command ? or NDK approach or simple copying one db to another from code. – siddhusingh Nov 14 '17 at 09:38
3

The error is passed on from the native code to Java, so you have to look at possible causes of SQLite corruption. Here's a webpage on the SQLite website that lists the ones due to bugs in SQLite, and here's another titled How to corrupt your database.

JRL
  • 76,767
  • 18
  • 98
  • 146
  • 2
    this seems to be all 'low-level' issues. Its not something I can do from the code. This means that something or someone (app killer) my be killing my app at some point so then the db is corrupted? After it has been corrupted, how do I fix it? Thanks – Daniel Benedykt Jun 02 '10 at 18:06