[Login]

How to repair a corrupted SQLite database

Assuming you have a corrupted SQLite database named data.db, you can take the following steps to repair it:

Step 1: Export the data

sqlite3 data.db
.mode insert
.output dump.sql
.dump
.exit

Step 2: Modify the dump.sql file

  1. Remove the 2nd line in the dump.sql file. It's something like BEGIN TRANSACTION
  2. Remove the last line in the dump.sql file. It's something like ROLLBACK

Step 3: Recreate the new database

sqlite3 new.db < dump.sql

Step 4: Integrity check

sqlite3 new.db "PRAGMA integrity_check"

Sources