1. sqlite的数据库以单文件的形式存放于磁盘中,比如demo.db文件;

  2. 表是以b+树的形式进行组织, 索引是b树组织的;

  3. sqlite_master表是sqlite的数据库系统表,存放所有的用户表的schema;

  4. 数据库文件是page的数组,每个page默认大小为1024个字节;

  5. 第1个page的头100个字节是数据库文件的头信息(元数据),具体参见表1;

  6. 第1个page同时是sqlite_master表的root page;

  7. sqlite_master的其中一个字段存放了其他用户表的root page number;

  8. b树的节点类型类型主要有四种,主要通过page的头信息进行表明,节点头信息参见表2;

  9. 每个b树内部是多个cell组成,每个cell的内容见表3;

  10. 删除数据并不会立即删除,回收到freelist中,如图1;

  11. 单个tuple的长度溢出一个page大小时,采用溢出页的方式,如图2;




表1:Database Header Format

OffsetSizeDescription
016The header string: "SQLite format 3\000"
162The database page size in bytes. Must be a power of two between 512 and 32768 inclusive, or the value 1 representing a page size of 65536.
181File format write version. 1 for legacy; 2 for WAL.
191File format read version. 1 for legacy; 2 for WAL.
201Bytes of unused "reserved" space at the end of each page. Usually 0.
211Maximum embedded payload fraction. Must be 64.
221Minimum embedded payload fraction. Must be 32.
231Leaf payload fraction. Must be 32.
244File change counter.
284Size of the database file in pages. The "in-header database size".
324Page number of the first freelist trunk page.
364Total number of freelist pages.
404The schema cookie.
444The schema format number. Supported schema formats are 1, 2, 3, and 4.
484Default page cache size.
524The page number of the largest root b-tree page when in auto-vacuum or incremental-vacuum modes, or zero otherwise.
564The database text encoding. A value of 1 means UTF-8. A value of 2 means UTF-16le. A value of 3 means UTF-16be.
604The "user version" as read and set by the user_version pragma.
644True (non-zero) for incremental-vacuum mode. False (zero) otherwise.
684The "Application ID" set by PRAGMA application_id.
7220Reserved for expansion. Must be zero.
924The version-valid-for number.
964SQLITE_VERSION_NUMBER



表2:B-tree Page Header Format

OffsetSizeDescription
01The one-byte flag at offset 0 indicating the b-tree page type.

A value of 2 (0x02) means the page is an interior index b-tree page.

A value of 5 (0x05) means the page is an interior table b-tree page.

A value of 10 (0x0a) means the page is a leaf index b-tree page.

A value of 13 (0x0d) means the page is a leaf table b-tree page.

Any other value for the b-tree page type is an error.
12The two-byte integer at offset 1 gives the start of the first freeblock on the page, or is zero if there are no freeblocks.
32The two-byte integer at offset 3 gives the number of cells on the page.
52The two-byte integer at offset 5 designates the start of the cell content area. A zero value for this integer is interpreted as 65536.
71The one-byte integer at offset 7 gives the number of fragmented free bytes within the cell content area.
84The four-byte page number at offset 8 is the right-most pointer. This value appears in the header of interior b-tree pages only and is omitted from all other pages.



表3:B-tree Cell Format

DatatypeAppears in...Description
Table Leaf (0x0d)Table Interior (0x05)Index Leaf (0x0a)Index Interior (0x02)
4-byte integer

Page number of left child
varint
Number of bytes of payload
varint

Rowid
byte array
Payload
4-byte integer
Page number of first overflow page



Sqlite3源码之数据库文件格式_java

图1:freelist page

Sqlite3源码之数据库文件格式_java_02


图2:溢出页


Sqlite3源码之数据库文件格式_java_03