SQLSTATE[HY000]: General error: 11 database disk image is malformed的解決方式 - 吉姆的電腦閣誌

吉姆的電腦閣誌

HTML5,Jquery,PHP,FreeBSD分享教學。電腦是用來節省時間及提升樂趣,不是用來把人綁住的。隨心而行,順著生命的脈動重新創造屬於自己的生活。

Breaking

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

2015-06-20

SQLSTATE[HY000]: General error: 11 database disk image is malformed的解決方式

SQLite是個很高效的資料庫,不過在應用時機則取決於開發目的,在許多Android App都是以SQLite為後端資料庫。最近開始接觸SQLite,在寫入時卻遇到一個錯誤訊息:
SQLSTATE[HY000]: General error: 11 database disk image is malformed
原本的資料庫狀況:
-rw-rw-r--  1 root www 713728  6 20 21:34 company.sqlite3
 
因為SQLite沒有像MySQL一樣提供Repair的指令,從這個錯誤訊息看來,應該就是磁碟異常,可能是硬碟空間不足或是寫入資料過程中發生問題....等,官方的說明:http://www.sqlite.org/lockingv3.html#how_to_corrupt
不過我很確定SERVER沒有斷過電或重開機,硬碟也沒什麼問題,所以只好土法鍊鋼,思考點是將資料dump出來重新建立一個,舊的檔案是company.sqlite3,新的是new.sqlite3。
解決的參考步驟如下:
  1.  備份並建立新的SQLite資料庫

    
    #sqlite3 company.sqlite3
    SQLite version 3.7.14.1 2012-10-04 19:37:12
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> PRAGMA integrity_check;
    *** in database main ***
    Main freelist: freelist leaf count too big on page 10
    Main freelist: invalid page number 218103808
    Page 3 is never used
    Page 12 is never used
    Page 13 is never used
    Page 14 is never used
    Page 15 is never used
    Page 16 is never used
    Page 17 is never used
    Page 18 is never used
    Page 19 is never used
    Page 20 is never used
    Page 21 is never used
    Page 22 is never used
    Page 23 is never used
    Page 24 is never used
    Page 26 is never used
    sqlite> .output tmp.sql
    sqlite> .dump
    sqlite> .quit
    
  2. 將dump出來的sql檔回寫到新的檔案

    
    #sqlite3 new.sqlite3
    SQLite version 3.7.14.1 2012-10-04 19:37:12
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> .read tmp.sql
    
    sqlite> .quit
    #ls -l
    total 798K
    -rw-rw-r-- 1 root www 713728  6 20 21:34 company.sqlite3
    -rw-r--r-- 1 root www  20480  6 20 21:53 new.sqlite3
    -rw-r--r-- 1 root www  27152  6 19 22:34 tmp.html
    -rw-r--r-- 1 root www  28700  6 20 21:52 tmp.sql
    
    很明顯的看到新的new.sqlite3檔案變小了,什麼原因就暫時不深入研究了,我猜可能是Journal日誌之類的,不過時間有限,先解決就好。
順道介紹一個Windows下的好工具,可以視覺化維護SQLite資料庫:sqlitebworser.exe

沒有留言:

Post Bottom Ad

Responsive Ads Here

Pages