问题
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.
回答1:
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.
回答2:
I would and do use
pragma integrity_check
This pragma does an integrity check of the entire database. The integrity_check pragma looks for out-of-order records, missing pages, malformed records, missing index entries, and UNIQUE, CHECK, and NOT NULL constraint errors. If the integrity_check pragma finds problems, strings are returned (as multiple rows with a single column per row) which describe the problems. Pragma integrity_check will return at most N errors before the analysis quits, with N defaulting to 100. If pragma integrity_check finds no errors, a single row with the value 'ok' is returned. (ref)
Why is this better than pragma quick_check
?
The pragma is like integrity_check except that it does not verify UNIQUE constraints and does not verify that index content matches table content. (ref)
In other words, pragma integrity_check
is more thorough.
来源:https://stackoverflow.com/questions/12418600/how-do-you-determine-if-an-sqlite-or-sqback-is-corrupt-in-java