SQLite3的数据库文件突然断电或者操作系统崩溃会损坏吗?

发布于 2024-08-05 11:21:57 字数 165 浏览 9 评论 0原文

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

非常感谢!

I open the database file and obtain a database connection using open() method of sqlite3 and the connection will not be closed until program exits. If there occurs an unexpected error such as computer's suddenly power-off or OS crash, will the mode of the database file be damaged, or its handle lost? More specifically, can it remain writable if I reboot my computer? BTW, I don't care about the data loss when errors occurs.

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2024-08-12 11:21:58

SQLite 是专门为防止这种情况而设计的。从官方 SQLite is Transactional 页面:

单个更改中的所有更改
SQLite 中的事务要么发生
完全或根本不,即使
将更改写入的行为
磁盘被中断

  • 程序崩溃,
  • 操作系统崩溃,或者
  • 电源故障。

上一段的主张是
在 SQLite 中进行了广泛的检查
使用特殊的回归测试套件
模拟的测试工具
对数据库文件的影响
操作系统崩溃和断电
失败。

您可能还对 SQLite 文章SQLite 中的原子提交感兴趣,如果您需要了解它们如何防止上述崩溃的具体细节。


Regarding writing after a crash: (from File Locking and Concurrency)

当进程正在进行数据库更新并且程序或操作系统崩溃或电源故障阻止更新完成时,就会创建热日志。热门期刊是一个例外情况。热日志的存在是为了从崩溃和电源故障中恢复。如果一切正常(即,如果没有崩溃或电源故障),您将永远不会得到热日志。

最糟糕的情况是您需要删除崩溃后留下的热日志。

SQLite is specifically designed to protect against this. From the official SQLite is Transactional page:

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.

You might also be interested in the SQLite article Atomic Commit in SQLite if you need to know the specific details on how they protect against crashes such as the above.


Regarding writing after a crash: (from 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.

The worst that can happen will be that you need to delete the hot journal that is left over after a crash.

如若梦似彩虹 2024-08-12 11:21:58

由于 Sqlite 符合 ACID,因此关闭电源不应该成为问题。

http://en.wikipedia.org/wiki/ACID

As Sqlite is ACID-compliant, a power-off shouldn't be an issue.

http://en.wikipedia.org/wiki/ACID

待天淡蓝洁白时 2024-08-12 11:21:58

突然断电可能会发生任何事情。不过,我建议 UPS 降低任何风险。

anything could potentially happen on sudden power off. However I'd suggest UPS to mitigate any risk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文