Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you determine if an .sqlite or .sqback is corrupt in Java?

I have been given a directory of directories each containing a collection of .sqlite and .sqback files that I must parse.

The problem is that I believe some of these files to be corrupt when I receive them because I get the error: ERR: [SQLITE_CORRUPT] The database disk image is malformed (database disk image is malformed) on my console when I try to process them. This only happens with some of the files. I have isolated a few and tried running my program on fresh copies of these bad files individually and they cause errors. Most of the files are fine though :)

I realized that there is a chance that I may indeed be given corrupt files to begin with so I would like a way to determine, prior to trying to parse them, which files are good and which are not.

I am writing in Java. I am only interested in the sqlite and sqback validation as I know my parser works. I am reusing it from a previous project.

Hint? Suggestions? Answers?

Many Thanks for the knowledge transfer.

like image 579
miss.serena Avatar asked Sep 14 '12 05:09

miss.serena


People also ask

How can I tell if SQLite database is corrupted?

To verify that you're truly suffering from database corruption, enter the following command into the shell: sqlite> PRAGMA integrity_check; If the response is anything other than ok, the database is integrity checks have failed and needs to be repaired.

Can SQLite be corrupted?

An SQLite database is highly resistant to corruption. If an application crash, or an operating-system crash, or even a power failure occurs in the middle of a transaction, the partially written transaction should be automatically rolled back the next time the database file is accessed.

How do I fix a corrupted SQLite database?

Download and configure Kernel SQLite Database recovery on your system. Start the tool, and the home screen will appear on the screen. Click Open to select the corrupt . db file you want to repair, and click Recover.

How can I see the structure of a table in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.


1 Answers

Run PRAGMA quick_check like a normal SQL query. The result is the same as a one-column table containing strings; for an intact database, you get a single row saying "ok", for a corrupt one, a bunch of error messages.

sqlite> pragma quick_check;
ok

after changing some bytes in the file:

sqlite> pragma quick_check;
*** in database main ***
Page 64: btreeInitPage() returns error code 11
On tree page 40 cell 23: Child page depth differs
On tree page 40 cell 24: Child page depth differs

There is no guarantee that errors will be found by this.

like image 165
CL. Avatar answered Oct 14 '22 07:10

CL.