How to recover a corrupt SQLite3 database?

前端 未结 12 2074
走了就别回头了
走了就别回头了 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 03:47

    I had an sqlite file that was corrupt that would show a symptom like this.

    select count(*) from corruptTable;
    return:38000;
    

    But when I would try to load the records with

    select * from corruptTable;
    

    It would only return 7 records.

    I tried several things, but these steps were the most successful.

    On a mac, open terminal and run these commands on your corrupt database. (these are sqlite3 commands, so you should be able to use other sqlite3 editors or similar commands in other systems).

    1 sqlite3 dbWithCorruptTable.sqlite (Obviously replace "dbWithCorruptTable" to your sqlite3 file that has the corrupt table)
    2 .mode insert
    3 .output dump_all.sql
    4 .dump
    5 .exit
    6 Manually edit the dump_all.sql file in a text editor and remove the transaction statements. Usually there is a "BEGIN TRANSACTION" statement on the 2nd line of the file and a "ROLLBACK" statement on the last line. Remove these and save the file
    

    These steps were taken from this website: http://www.dosomethinghere.com/2013/02/20/fixing-the-sqlite-error-the-database-disk-image-is-malformed/

    0 讨论(0)
  • 2020-11-28 03:55

    I was able to repair my Chrome history file (which is a sqlite3 database file) this way:

    sqlite3.exe History ".backup History-new"
    
    0 讨论(0)
  • 2020-11-28 03:58

    UPDATE: There is now an automatic method that is built into SQLite: .recover

    Sometimes, the corruption is only or mostly in indexes, in which case it would be possible to get some or most records by trying to dump the entire database with .dump, and use those commands to create a new database:

    $ sqlite3 mydata.db ".dump" | sqlite3 new.db
    

    However, this is not always possible.

    The easiest and most reliable way is to restore the database file from the backup.

    0 讨论(0)
  • 2020-11-28 03:58

    With Sqlite 3.29.0 a new .recover command has been introduced to the CLI:

    Add the ".recover" command which tries to recover as much content as possible from a corrupt database file.

    sqlite3 broken.db ".recover" | sqlite3 new.db
    
    0 讨论(0)
  • 2020-11-28 03:58

    This worked for me:

    Download the sqlite3 tools package from here and put it into any folder. Put your corrupted database in the same folder.

    Open a command prompt.

    Type the following:

    sqlite3.exe

    (Press Enter)

    NAME_OF_YOUR_CORRUPTED_DATABASE> ".dump" | sqlite3 new.db

    (Press enter)

    All the other solutions didn't work for me.

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

    The pragma writable_schema disables some integrity checks, so this two commands might also do the trick, keeping db customizations in place:

    PRAGMA writable_schema=ON;
    VACUUM;
    
    0 讨论(0)
提交回复
热议问题