Repairing SQLite Databases

If you use SQLite databases you may run into a database in a crashed state – something has happened that has corrupted some component of the DB and it required a repair. You’ll often see a message like: “SQLSTATE …”

The easiest way to repair this is to “recreate” the database by doing a dump from the old one into a new one. First, move the old one so you have a copy of it.

mv database.db database-broken.db

Now that you have moved it, run this command to dump the data from database-broken.db to database.db, effectively recreating the database:

echo ".dump" | sqlite3 database-broken.db | sqlite3 database.db

This is also a great way to reduce the size of a database.

Got Something To Say?

Your email address will not be published. Required fields are marked *