1.断电情况下数据库文件会损坏?你测试过?由于项目需要,这几天也在调查这个问题。我在虚拟机环境下插入数据过程中强制关闭虚拟机模拟断电操作,数据库没有被损坏,重新打开系统,数据库照样可以访问。给一个SQLite官方document的链接How To Corrupt An SQLite Database File(http://www.sqlite.org/howtocorrupt.html)其中第一段如下:
An SQLite database is highly resistant to corruption. If an application crash, or an operating-system crash, or even a power failure occurs in the middle of a transaction, the partially written transaction should be automatically rolled back the next time the database file is accessed. The recovery process is fully automatic and does not require any action on the part of the user or the application.
也就是说在写事务过程中断电是不会损坏数据库文件的。但并不是说断电就一定不会损坏数据库文件,只是说写事务修改数据库过程中断电SQLite的操作是不会损坏数据库文件的,如果断电对硬盘介质造成损坏或影响了文件系统,那是有可能间接造成数据库文件损坏的,这种情况下什么文件都有可能受损,是不受我们控制的。至于为什么断电不会损坏数据库,你可以调查一下hot journal或者Write-Ahead Log,在此就不多说了。
2.SD卡更操蛋。具体体现在数据从缓存中同步到磁盘过程中。
3.在问题1里说的断电不会由于SQLite写事务对数据库文件造成损坏,其实现机制就是利用了事务+日志机制,想具体了解就看hot journal或者Write-Ahead Log部分。
建议先看那篇文档,文档中描述了其它几种有可能造成数据库文件损坏的原因和解决办法。
如果我的理解有什么地方有问题欢迎探讨。