Introduction
SQLite is a complete SQL database held in a single file, read and written by a small library built into the program that uses it. This page is the file of SQLite 2, as versions 2.6.0 (18 July 2002) to 2.8.17 (19 December 2005, the last SQLite 2) write it. SQLite 3.0.0 replaced it in June 2004 with a new design, and the 2.8 releases carried on beside it until 2.8.17.
The two formats are not compatible: SQLite 3 cannot open an SQLite 2 file, and
SQLite 2 cannot open an SQLite 3 one. SQLite 2 files still turn up in old PHP
applications, which shipped with SQLite 2 support, and in old program data. They are
usually named .db or .sqlite, like SQLite 3 files.
SQLite 2 stores every value as text, uses fixed 1,024-byte pages, and writes its numbers in the byte order of the machine that made the file. Its b-tree keeps cells anywhere on a page, chained in key order, with entries on interior pages as well as leaves. See all the SQLite formats.
A file dropped on any page of this site opens in the explorer, which shows its pages, cells and records, the schema, and a rows preview of every table.
Structure
The file is a run of 1,024-byte pages, numbered from 1. Page 1 holds the
database header; page 2 is the root of
the schema table,
sqlite_master. Every other page is a
b-tree page of a table or index,
an overflow page or a
freelist page. A page’s kind is not
written on it; it follows from what points to it.
Every number is in the byte order of the machine that wrote the file, which the header’s magic number shows. Two things are fixed whatever that order: a table’s rowid keys are big-endian, and the offsets inside a record are little-endian.
0Page 1 the 48-byte header text, the byte-order magic, the freelist and nine meta values; the rest is unused1,024 bytes1024Page 2 the root ofsqlite_master, which lists every table, index, view and trigger1,024 bytes2048Pages 3 onwards b-tree pages of cells, overflow pages and freelist pages1,024 bytes each
A table is a b-tree keyed by the 4-byte rowid, and its data is a record of zero-terminated text values. An index is a b-tree whose keys are the indexed values, each with a type letter, followed by the row’s key.
Older SQLite 2 files
SQLite 2 records its layout in a file format number, meta value 2 at offset 64. The releases before 2.6.0 wrote layouts that differ from this one in how keys and cells are stored, so by this site’s rule (a version that changes existing structures is a separate format) each is a format of its own:
| SQLite | Format number | What differs |
|---|---|---|
| 2.0.0 to 2.0.8 (September to November 2001) | – | the header text says “SQLite 2.0”; the cell header has a 32-bit data size and no high bytes |
| 2.1.0 (12 November 2001) | 0 | the “2.1” text and 24-bit sizes, but rowid keys are big-endian with no bit flipped, and index fields have a zero-byte prefix |
| 2.2.0 (22 December 2001) to 2.5.6 (7 July 2002) | 1, then 2 from 2.4.0 | rowid keys with the top bit flipped, as here, but index keys built the old way |
| 2.6.0 (18 July 2002) | 3 | index keys rebuilt with the type letters a, b and c: the start of this format |
| 2.7.0 (25 August 2002) | 4 | numbers in text columns are indexed as text: which letter a value gets, not how keys are laid out, so the same format |
The SQLite 2.6.0 changelog calls its change “an incompatible file format change”. From 2.6.0, the library converts an older file when it opens it, “automatically and irreversibly”, by rebuilding every table and setting the format number to 4. Their cells, keys and records are described on the page Older layouts, and the explorer reads them, showing which layout a file uses.
SQLite 2.8.0 (16 February 2003) added a safety level in meta value 4, a value that was 0 before, and a new journal format. Neither changes the database layout.
Byte order
Byte order is not a version. Every SQLite 2 release wrote the file in its machine’s own order: big-endian on PowerPC Macs and SPARC, little-endian on x86. So both orders exist for every version, and the byte-order magic at offset 48 tells them apart. Before 2.6.3 (13 August 2002) a file could only be read on a machine of the same byte order; from 2.6.3 SQLite reads either. The explorer reads both.
Telling it apart
The explorer’s parser for SQLite 2 checks two things, and then the layout:
- the file starts with the 48 bytes
** This file contains an SQLite 2.1 database **and a zero byte (the text says 2.1 in every version from 2.1.0 to 2.8.17), or the same text with 2.0 for an SQLite 2.0 file; - the 4 bytes at offset 48 are 0xDAE37528 in one byte order or the other;
- the file format number at offset 64 says which layout it uses: 3 or 4 for this one, 1 or 2 for SQLite 2.2 to 2.5, and 0 in a file with tables for SQLite 2.1 (0 is also an empty new file). A number above 4 is not one SQLite 2 ever wrote.
The older layouts differ only in details; the explorer reads every one and says which it found.
A file that starts with SQLite format 3 and a zero byte is an
SQLite 3 database.
Converting to SQLite 3
SQLite 3 cannot read this format. The usual way to convert a file is to dump it as SQL
with the old sqlite shell and load the result into SQLite 3:
sqlite old.db .dump | sqlite3 new.db
Journals
While a transaction writes to the database, SQLite 2 keeps the original pages in a
file named like the database with -journal added. Its records hold
1,024-byte pages, and three journal formats were used:
| Journal format | Starts with | Layout |
|---|---|---|
| 1 | D9 D5 05 F9 20 A1 63 D4 | numbers in the machine’s byte order; in 2.8 used only for testing |
| 2 | D9 D5 05 F9 20 A1 63 D5 | big-endian: the magic, the original database size in pages, then records of a page number and a page |
| 3 (from 2.8.0) | D9 D5 05 F9 20 A1 63 D6 | big-endian: the magic, a record count (0xFFFFFFFF: work it out from the file size), a checksum start value and the original size, then records of a page number, a page and a checksum (the start value plus the page number) |
A record whose page number is 0 ends the playback. The explorer reads all three
journal formats; see SQLite 2
journals. SQLite 3’s rollback journal is a different format, ending its magic in
D7.
Also used for
- Windows thumbnail cache (Thumbs.db) – Windows thumbnail caches: a folder's hidden Thumbs.db (a Compound File of small pictures) and the per-user thumbcache_*.db files of Windows Vista and later.
Variants
- SQLite 3 database – SQLite 3.0.0 (2004) to today: b-tree pages, typed values, UTF-8 or UTF-16 text
- SQLite 2 database – SQLite 2.6 to 2.8 (2002 to 2005): 1,024-byte pages, every value text
Links
Software
- SQLite 2.8.17 – the last SQLite 2 library and its
sqliteshell, the only software known to open these files, built from source (C)
Reference
- SQLite Version 3 Overview – the SQLite page on the changes from SQLite 2 and on converting old databases
- SQLite 2.8.17 source – the source this reference is checked against, with SQLite’s own 2.x file format document, www/fileformat.tcl
- Release history – SQLite’s changelog, including the 2.x releases and their file format changes