在 Sqlite 数据库中. db-shm 和. db-wal 扩展名是什么?

在运行了一些关闭数据库、删除数据库并将其替换为测试装置的测试之后,我看到应用程序及其数据库文件的状态出现了一些奇怪的行为。当我在调试 PC 上使用工具检查数据库文件时,它与应用程序本身似乎报告的内容不匹配。这种奇怪的行为可能与 这个窃听器有关。

我注意到有两个文件的基本名称与数据库相同(扩展名为 .db)文件扩展名是 .db-shm.db-wal,每个扩展名都比 .db文件的时间戳更新。

我假设这些是某种类型的临时文件。然而,我想知道如果应用程序终止,他们不应该被删除吗?更重要的是,我假设在应用程序被操作系统终止之前,它们中存储的任何数据都会在 .db文件中进行更新。是这样吗?

98322 次浏览

You are correct, these are temporary files created by SQLite. If you are manually deleting the main db you should probably delete these too. From what I can gather the WAL is a replacement for the rollback journal that enables SQLite to rollback changes when a transaction fails. How SQLite uses them and why they are kept around for so long is up to the authors of SQLite but in general SQLite seems pretty rock solid so I wouldn't worry too much about them. For more info take a look here:

http://www.sqlite.org/fileformat2.html#walindexformat

These files are a new feature of SQLite 3.7. I'm not sure if their existence relates to the bug you point out but the bug report suggests a work-around anyway.

UPDATE:

Better documentation about the WAL is here:

https://www.sqlite.org/wal.html

The contents of the WAL are periodically moved to the DB file but this is not guaranteed to occur each time the process exits. Thus when WAL is enabled each SQLite DB consists of two files on disk that must be preserved, both the .db file and the .db-wal file.

The .db-shm file is a shared memory file that contains only temporary data.

I do not yet have enough reputation to just add a comment to satur9nine's answer, so I'll pile on here.

As per the SQLite docs, the DB-SHM file is a Shared Memory file, only present when SQLite it running in WAL (Write-Ahead Log) mode. This is because in WAL mode, db connections sharing the same db file must all update the same memory location used as index for the WAL file, to prevent conflicts.

As for WAL file, as hinted above, it is a write log/journal, useful for commits/rollback purposes.

Make sure that you have closed cursor properly into SELECT operation. Sometimes SQLiteOpenHelper creates .db-shm and .db-wal extensions database due to unclosed Cursor.