修复SQLite

转自:http://zhiwei.li/text/2010/08/%E4%BF%AE%E5%A4%8Dsqlite-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

请参考  http://www.sqlite.org/faq.html#q21

http://www.sqlite.org/sqlite.html

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;'

你可能感兴趣的:(修复SQLite)