This is a follow up question to a previously answered post: Is there a command line utility for validating SQLite databases in Linux?
If a database is producing the
My method is similar, prevents a error rollback script:
sqlite3 database.db ".dump" | sed -e 's|^ROLLBACK;\( -- due to errors\)*$|COMMIT;|g' | sqlite3 database.new
I have fixed database corruption caused by missing indexes with these steps and they are working for me.
DROP Index: sqlite drop index command
Run vacuum Sqlite vacuum command
Recreate index again : Sqlite create index
If the database is seriously corrupt, the .dump
will contain errors, and some data may be lost.
For more complex data schemas, this will mean orphaned and/or partial records which may confuse the application.
It may be preferable to .dump
to a file, then use a text editor to remove problematic rows. Search for ERROR
within the dump file.
The following fix worked to repair my database without running any command line tools.
I got the "database disk image is malformed" error message when I was working with one of my tables so I ran [PRAGMA integrity_check] which returned
Main freelist: free-page count in header is too small
On tree page 16198 cell 1: 2nd reference to page 14190
Page 16988 is never used
Page 46637 is never used
row 4493 missing from index indexname1
row 4493 missing from index indexname2
row 4493 missing from index indexname3
I first saved the schema for the table with the bad indexes so I could recreate those indexes. I then dropped the indexname 1, 2, and 3 indexes with the [drop index _] command. I exported my tables one by one to JSON files and then truncated each table. Running the integrity check at that point was successful. I then added the three indexes back with the [create index _] command and imported each table's records from their JSON file export. Running the integrity check command is still returning "ok" with all of the records restored.
I know this is an old question, but I would still like to share my solution. My problem was that a sqlite3 database for kodi(xbmc) was corrupted.
.dump did not work in my case
file is encrypted or is not a database
What worked was the following:
I fixed it with the following steps after I even could not remove single corrupt rows by sql statement, only all.