1. 程式人生 > >Sqlite資料庫報錯——The database disk image is malformed

Sqlite資料庫報錯——The database disk image is malformed

轉載:http://zhiwei.li/text/2010/08/修復sqlite-database-disk-image-is-malformed/

執行某些SQL語句出錯database disk image is malformed

說明SQLite的內部資料格式,已經損壞

sqlite> PRAGMA integrity_check;

*** in database main ***

Main freelist: 3 of 3 pages missing from overflow list starting at 0

排除磁碟空間不夠的原因
修復步驟
$ sqlite3 backup.sqlite
sqlite> .output “_temp.tmp”
sqlite> .dump
sqlite> .quit
$ sqlite3 new.sqlite
sqlite> .read “_temp.tmp”
sqlite> .quit
就將錯誤的backup.sqlite修復為new.sqlite了

另一種更快的修復方法

$echo “.dump” | sqlite3 old.db | sqlite3 new.db

直接將 old.db 修復為 new.db

或者

sqlite3 newsfeed.db .dump > newsfeed.sql

sqlite3 newsfeed.db < newsfeed.sql

PHP使用popen呼叫sqlite shell來實現修復

$handle = popen(“mv ../backup.sqlite bad.sqlite && sqlite3 bad.sqlite .dump | sqlite3 good.sqlite && mv good.sqlite ../backup.sqlite”, ‘r’);
$read = fread($handle, 4096);
echo $read;
pclose($handle);

壓縮

sqlite3 my.sqlite 'VACUUM;'