SQLite 2 数据库 header 格式与 SQLite 3 的格式不同吗?

Is SQLite 2 database header format different from that of SQLite 3?

我通过读取文件的前16个字节来判断文件是否为SQLite数据库(如https://www.sqlite.org/fileformat.html所述)。如果我读过 SQLite format 3,那么它就是一个 SQLite 数据库。

我没有示例 SQLite 2 数据库,所以我不知道 header 它有什么,而且我在文档中找不到它。有人知道 header 是什么吗?

虽然没有明确说 header 已经改变,但我相信前 16 个字节会有所不同,至少 3 (第 15 个字节 x'33') 可能不是 3,这足以让 SQLite3 认为文件不兼容。

您可以通过History Of SQLite Releases and read the readme at SQLite Source Repository获取一个版本。编译并执行测试以适应。

您可能还会对 File Format Changes in SQLite and additionally SQLite Version 3 Overview 感兴趣。

SQLite 2 数据库以

开头
** This file contains an SQLite 2.1 database **

我只见过 2.1 数据库,但可能 2.0 数据库以 2.0 开头而不是 2.1

对于 SQLite 2 数据库,这些是基于 SQLite 2.0.0 (GitHub mirror, official repo and SQLite 2.8.0 (GitHub mirror, official repo, on SourceForge) 源代码的可能前缀(作为引用的 C 字符串,没有隐式尾随 [=12=]),常量 zMagicHeader 在源文件中 src/btree.c:

"** This file contains an SQLite 2.0 database **\x00\xda\xe3\x75\x28"
"** This file contains an SQLite 2.0 database **\x00\x28\x75\xe3\xda"
"** This file contains an SQLite 2.1 database **\x00\xda\xe3\x75\x28"
"** This file contains an SQLite 2.1 database **\x00\x28\x75\xe3\xda"

对于 SQLite 3 数据库,这些是可能的前缀(作为引用的 C 字符串,没有隐式尾随 [=12=]),基于 official file format documentation and SQLite 3.0.0 (GitHub mirror, official repo)源代码,常量 zMagicHeader在源文件中 src/btree.c:

"SQLite format 3\x00\x00\x01"
"SQLite format 3\x00\x02\x00"
"SQLite format 3\x00\x04\x00"
"SQLite format 3\x00\x08\x00"
"SQLite format 3\x00\x10\x00"
"SQLite format 3\x00\x20\x00"
"SQLite format 3\x00\x40\x00"
"SQLite format 3\x00\x80\x00"

相关版本号取自official file format changes document.