Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a command line utility for validating SQLite databases in Linux? [closed]

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

like image 355
linsek Avatar asked Aug 15 '13 18:08

linsek


People also ask

How check SQLite database in Linux?

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.

How do I query a SQLite database from the command line?

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.

How can I tell if a SQLite database is open?

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.

Which command is used to create or open an existing database in SQLite?

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.


2 Answers

You can do something like this:

sqlite3 database.db "PRAGMA integrity_check"
like image 60
Yura Beznos Avatar answered Oct 12 '22 11:10

Yura Beznos


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
like image 42
Oscerd Avatar answered Oct 12 '22 10:10

Oscerd