Fixed several bug

Fail on write, rewrite db file and others bugs...