Can I deliberately mess up my sqlite3 database for testing?

I am running a small but essential application on an android phone. The sqlite database is used as the main data store. After more than a year of near-flawless operation, it suddenly crashed at a critical moment a few days ago as a result of a bad "disk problem with the database drive being corrupted." This caused some confusion as I couldn't access the system to fix it for several hours.

I know that such situations should be very rare . But now I am coding the code to recover from such a situation more gracefully, since I cannot allow this to happen again (and it could be due to the hardware of the phone, in which case it can now start more often - but again I can't afford to crash again).

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

My question is, is there something I can do for a sqlite database that will result in a "database disk image mangled" error? Then I can check the recovery code.

+3


source to share


2 answers


Can you do it.

  • Just copy your working database file to assets.
  • Then, once the application starts, copy the database file through code byte by byte, and discard some bytes when copying, you will end up with a corrupted database.


You can also try something else:

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

+1


source


There is an article in the SQLite docs named How to Corrupt SQLite Database File

SQLite database files are regular files on disk. This means that any process can open the file and overwrite it with garbage. There's nothing the SQLite library can do to protect against this.



If you are writing garbage data to a file I think you can achieve what you want

+2


source







All Articles