【发布时间】:2014-03-23 02:11:41
【问题描述】:
我编写了一个解析 SQLite 文件的程序,我可以解析来自 b-tree 页面的所有数据以记录、列和值,但我需要解析表的架构,我发现类似于存储在第 1 页中的 数据库架构(根页面) 我可以用Hex Editor 看到它,我发现sqlite_master 的结构,我完全按照http://sqlite.org/fileformat2.html 中的说明阅读它
我想知道如何在 db 文件中找到 sqlite_master 表的第一个字节,如何检测架构的起始字节? SQLite DB Header 中有什么相关的吗?
编辑 1(更多信息):
例如: 我用十六进制编辑器打开了 sqlite db,(如果你检查我的页面大小是 4096 字节并且我在图像中标记了页眉):
我标记了以 05 开头的根页眉表示 the page is an interior table b-tree page,请检查 B-tree 页眉格式 (http://sqlite.org/fileformat2.html) 并且它有 5 个单元格,您可以在此单元格中看到它指针数组:0FFB、0FF6、0FF1、0FEC、0FE7(在结束标头之后开始)并且所有单元格都有 5 个字节并从 0FE7 开始,然后您可以在图片中看到的模式(在文本部分)从 232~240 开始,并且我在不同的地方检查其他数据库和架构...
编辑 2:
您可以从https://www.dropbox.com/s/lanky02kneyb74w/31bb7ba8914766d4ba40d6dfb6113c8b614be442下载示例文件
编辑 3:
在我的文件中你可以看到
$ hexdump -C 31bb7ba8914766d4ba40d6dfb6113c8b614be442
00000000 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 |SQLite format 3.|
00000010 10 00 02 02 00 40 20 20 00 00 00 02 00 00 00 3f |.....@ .......?|
00000020 00 00 00 00 00 00 00 00 00 00 00 47 00 00 00 04 |...........G....|
00000030 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 |................|
00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 |................|
00000060 00 2d e2 25 05 00 00 00 05 0f e7 00 00 00 00 3d |.-.%...........=|
00000070 0f fb 0f f6 0f f1 0f ec 0f e7 08 7f 07 9d 08 3c |...............<|
00000080 07 01 06 22 05 92 04 fe 03 fc 04 c1 03 4d 02 b8 |...".........M..|
00000090 02 0a 02 75 01 32 01 c7 00 e9 00 e9 00 00 00 00 |...u.2..........|
000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000000e0 00 00 00 00 00 00 00 00 00 47 18 06 17 5b 35 01 |.........G...[5.|
000000f0 00 69 6e 64 65 78 73 71 6c 69 74 65 5f 61 75 74 |.indexsqlite_aut|
00000100 6f 69 6e 64 65 78 5f 41 42 4d 75 6c 74 69 56 61 |oindex_ABMultiVa|
00000110 6c 75 65 45 6e 74 72 79 4b 65 79 5f 31 41 42 4d |lueEntryKey_1ABM|
00000120 75 6c 74 69 56 61 6c 75 65 45 6e 74 72 79 4b 65 |ultiValueEntryKe|
Page Header ( offset 64)
05 <- interior table b-tree page
0000 <- Byte offset into the page of the first freeblock
0005 <- Number of cells on this page
0FE7 <- Offset to the first byte of the cell content area
00 <- Number of fragmented free bytes
0000003D (61) <- The right-most pointer
Cell Array Pointers & Cell Contents:
(Table Interior Cell Format)
Cell Pointer| Page number of left child | Rowid
------------|---------------------------|-------
0FFB | 0000001A (26) | 15
0FF6 | 0000001C (28) | 2D
0FF1 | 00000031 (49) | 3C
0FEC | 00000039 (57) | 48
0FE7 | 0000003C (60) | 4C <- equal to (Offset to the first byte of the cell content area) in page header
【问题讨论】:
-
您对该文件有什么具体问题?
-
@CL。 tanx 这么多花花公子试图解决我的 prb,如果你还记得我,你跟着我写 SQLite 解析器来提取数据和恢复已删除的记录 (stackoverflow.com/questions/21628240/…),我写了它,它的工作正常并提取所有值,但现在,我只是想加载表的架构,我怎么能找到架构?,如果你阅读第 1 页标题或单元格指针数组(根页),你可以
t find offset of schema, and cant 加载 sqlite_master 表.. -
@CL.,如果第一个表像我的文件一样是
interior table b-tree page,请告诉我如何在 sqlite db 文件中找到架构偏移量......!? -
更新了答案。架构存储在
sqlite_master表的sql列中。