sqlite - SQLite3的数据库文件在突然断电或系统崩溃时会损坏吗?

标签 sqlite crash data-integrity acid fault-tolerance

我打开数据库文件并使用 sqlite3 的 open() 方法获取数据库连接,并且在程序退出之前不会关闭连接。如果出现意外错误,如计算机突然断电或操作系统崩溃,数据库文件的模式是否会损坏,或句柄丢失?更具体地说,如果我重新启动计算机,它能否保持可写状态?顺便说一句,我不关心发生错误时的数据丢失。

非常感谢!

最佳答案

SQLite 专门旨在防止这种情况发生。来自官方SQLite is Transactional页:

All changes within a single transaction in SQLite either occur completely or not at all, even if the act of writing the change out to the disk is interrupted by

  • a program crash,
  • an operating system crash, or
  • a power failure.

The claim of the previous paragraph is extensively checked in the SQLite regression test suite using a special test harness that simulates the effects on a database file of operating system crashes and power failures.

您可能还对 SQLite 文章 Atomic Commit in SQLite 感兴趣如果您需要了解有关它们如何防止上述崩溃的具体细节。


关于崩溃后的写作:(来自File Locking and Concurrency)

A hot journal is created when a process is in the middle of a database update and a program or operating system crash or power failure prevents the update from completing. Hot journals are an exception condition. Hot journals exist to recover from crashes and power failures. If everything is working correctly (that is, if there are no crashes or power failures) you will never get a hot journal.

可能发生的最坏情况是您需要删除崩溃后遗留下来的热日志。

关于sqlite - SQLite3的数据库文件在突然断电或系统崩溃时会损坏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452790/

相关文章:

Android SQLite SQLiteOpenHelper IllegalStateException - 数据库已经关闭错误

java - UNIQUE 约束失败 : sqlite database : android

vb.net - VB.NET SQLite 查询中的 StackOverflow

c# - 大型数据库表的数据完整性

php - 高效的数据导入?

android - 请求增量注释处理

ios - UITableView IBOutlet 在 ViewDidLoad 上崩溃,Apple Beta 审查小组不适合我

ios - 涉及 CGPathAddLineToPoint 的不可重现的崩溃错误

android - Android无法更改按钮文字

mysql - 维护MySQL和Redis之间的数据完整性