4

I am running a small but critical application on an Android phone. It uses an sqlite database as its main data storage. After over a year of near flawless functioning, it suddenly crashed at a critical point a few days ago as a result of the infamous "database disk image is malformed" problem. This caused some embarassment as I couldn't access the system to fix it for a few hours.

I'm aware that such situations should be very rare. But I'm now coding in some code to more gracefully recover from such a situation, since I can't afford to have it happen again (and it may be due to the phone's hardware, in which case it might now start to happen more frequently - but again I can't afford a crash even once more).

However, to test my recovery code, I need to generate the same error, and due to a stupid mistake I lost the copy of the database that was creating the problem.

My question is - is there something I can do to an sqlite database that would result in the "database disk image is malformed" error? I can then test my recovery code.

ShankarG
  • 1,105
  • 11
  • 26

2 Answers2

2

The SQLite docs have an article named How To Corrupt an SQLite Database File

SQLite database files are ordinary disk files. That means that any process can open the file and overwrite it with garbage. There is nothing that the SQLite library can do to defend against this.

If you write garbage data to the file, I guess you can achieve what you want

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Aha, I didn't think of the obvious. I'll try that, I wonder if it will generate a malformed error or something else though. – ShankarG Nov 17 '14 at 06:43
  • @ShankarG I haven't tried that myself. I just pasted the most probable cause from the article :) – An SO User Nov 17 '14 at 06:44
  • Very basic question - in a linux system, how do you just change some bytes in a file? dd overwrites the file. – ShankarG Nov 17 '14 at 06:48
  • http://stackoverflow.com/questions/7290816/how-to-overwrite-some-bytes-of-a-binary-file-with-dd – An SO User Nov 17 '14 at 07:04
1

You can do it.

  1. Simply copy your working database file in assets.
  2. Then once application runs, copy the database file through code bytes by bytes and discard some bytes while copying, you will get a corrupted database.

You can try something else as well :

Just create a text file. Then rename it to *.db file and try to open the file in android assuming its a database file. I hope you will get the same exception.

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44