I am looking for a command line utility to validate SQLite databases. I ran into a situation in some inherited code where an application fails to startup because an attempt to access a database produced the following error:
database disk image is malformed
So I need to instrument some validation code in the application. Additionally, though, I need a tool that I can run from the Linux prompt to tell me simply if the database is corrupt or not.
Thanks
If you are using Linux or a Mac, open a terminal window instead a command prompt. Open a command prompt (cmd.exe) and 'cd' to the folder location of the SQL_SAFI. sqlite database file. run the command 'sqlite3' This should open the SQLite shell and present a screen similar to that below.
Start the sqlite3 program by typing "sqlite3" at the command prompt, optionally followed by the name the file that holds the SQLite database (or ZIP archive). If the named file does not exist, a new database file with the given name will be created automatically.
To see if it's opened, check the pointer to see if it's NULL (which you of course must initialize it to, or else it has an unspecified value). After you close it, assign it to NULL again, and there's your "is open" flag. By the way, creating a DatabaseHandle class using RAII is strongly suggested.
In SQLite, sqlite3 command is used to create a new SQLite database. You do not need to have any special privilege to create a database.
You can do something like this:
sqlite3 database.db "PRAGMA integrity_check"
You can use PRAGMA integrity_check on the database.
If the Database is corrupted you can use this SQLite command:
cd $DATABASE_LOCATION
echo '.dump'|sqlite3 $DB_NAME|sqlite3 new_repaired_$DB_NAME
mv $DB_NAME corrupt_$DB_NAME
mv new_repaired_$DB_NAME $DB_NAME
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With