How to recover a corrupt SQLite3 database?

前端 未结 12 2075
走了就别回头了
走了就别回头了 2020-11-28 03:46

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

相关标签:
12条回答
  • 2020-11-28 04:01

    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
    
    0 讨论(0)
  • 2020-11-28 04:05

    I have fixed database corruption caused by missing indexes with these steps and they are working for me.

    1. DROP Index: sqlite drop index command

    2. Run vacuum Sqlite vacuum command

    3. Recreate index again : Sqlite create index

    0 讨论(0)
  • 2020-11-28 04:08

    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.

    0 讨论(0)
  • 2020-11-28 04:09

    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.

    0 讨论(0)
  • 2020-11-28 04:10

    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:

    1. Made a backup of the old db File
    2. Let kodi create a new db File
    3. Checked on this site for the header format of sqlite files
    4. Opened both files with a hex editor (bless) and checked the first 96 Bytes
    5. The first 40 bytes where different so i copied the first 40 bytes from the new db file to the old db file
    6. After doing this, my database file worked again !!!
    0 讨论(0)
  • 2020-11-28 04:10

    I fixed it with the following steps after I even could not remove single corrupt rows by sql statement, only all.

    1. Open the *.db with sqlite browser and PRAGMA integrity_check; indicates the corrupt table.
    2. export this table as csv by sqlite browser
    3. delete all in table. DELETE FROM [tablename]
    4. Import csv with sqlite browser and select in import settings -> Advanced -> Conflict strategy -> Ignore Rows
    5. After that my Integrity Check was OK
    0 讨论(0)
提交回复
热议问题