SUBMIT A FILE TYPE CONTACT US SUPPORT ABOUT US

What Is a SQLite File? The Single-File Database, and How to Open, Check and Repair One

A SQLite file is a database in a single file. The integrity check passed 4 of the 21 damaged copies we made of one, and one of them had a changed value. Jump to your question below, or work through it in order.

30 min read Updated November 2026 Checked against sources and tests

What Is a SQLite File? A database in a single file, and why it is on every device

Short answer

SQLite files are databases in a single file, made by the SQLite engine. They store tables, indexes, triggers and views in equal-size pages and start with the text SQLite format 3. One SQLite file works on every platform.

We tested 20 SQLite files with two engines, the sqlite3 shell and pysqlite3. Both opened all 20. We then damaged one file in 21 ways, and the integrity check passed 4 copies. One of those 4 had a changed value.

To see inside one, use the open steps. To make a SQL or CSV file from one, use the export steps. If your SQLite file will not open, begin with the causes.

A SQLite file is a database in a single file, made by the SQLite engine. It stores tables, indexes, triggers and views in equal-size pages. Every SQLite file starts with the text SQLite format 3, and it works on every platform.

Picture a file named contacts.db that an app made on your phone. You copy it to a laptop and open it, and every table is there. Now picture a second file with the same name that no program will open. The first bytes name which is which.

Wikipedia says D. Richard Hipp designed SQLite in spring 2000 and released version 1.0 in August 2000. Its code is in the public domain. The article lists the endings .sqlite, .sqlite3, .db, .db3, .s3db and .sl3, and the media type application/vnd.sqlite3.

The Library of Congress description calls version 3 the native format of SQLite since 2004. It says SQLite files store a complete SQL database in one disk file, with tables, indexes, triggers and views. The record adds that no official ending exists.

Apps use the format everywhere. Wikipedia names Android, iOS, macOS and Windows 10 onward among the systems that include it, and it names Chrome, Firefox and Safari among the browsers. So most people own SQLite files that they never see.

Where our SQLite files come from

We studied 20 files. 8 are public test or sample databases from five GitHub projects. Chinook gave 1, Northwind gave 1, Datasette gave 1, GDAL gave 3 and sqlite-jdbc gave 2. We made the other 12 with Python.

The SQLite files are 4,096 to 24,702,976 bytes long, and the median is 100,096 bytes. They are sample databases and test files, not databases from a phone or a browser. We opened none in a desktop app, because our lab uses Linux. We make no claim about what an app shows.

So this page studies how SQLite engines treat SQLite files. Our two engines are the sqlite3 shell 3.45.1 and pysqlite3 with SQLite 3.51.1. We used the file command for a quick look and Python for scripts.

SQLite file facts at a glance

Quick facts about SQLite files
Property Value
Meaning One database in one file, made by SQLite.
Endings .sqlite, .sqlite3, .db, .db3, .s3db and .sl3, per Wikipedia. Our files end in .db or .sqlite.
Maker D. Richard Hipp, with version 1.0 in August 2000, per Wikipedia.
Media type application/vnd.sqlite3, per the Library of Congress. It calls application/x-sqlite3 deprecated.
First bytes 53 51 4c 69 74 65, the word SQLite, in all 20 of our files.
Page size A power of two from 512 to 65,536 bytes, per sqlite.org. Our files use 512, 1,024, 4,096 or 65,536.
Readers The sqlite3 shell and Python, both tested. FileInfo lists more programs.
Our tests 20 files, 21 damaged copies, 2 engines, a check script of 22 lines and a repair script of 14 lines.

Is your file an Access database? The MDB file overview covers that format.

Which Kind of File Is Yours? Three kinds, and the first bytes name the kind

Three kinds of file show up around SQLite databases in our tests. They are a SQLite file, a SQLite file with a -wal file beside it and a file that engines refuse. First bytes and neighbor files name the kind, and the ending does not.

Anyone can change the ending of SQLite files. All 20 of our test files start with the SQLite text, and both engines opened all 20. The kinds differ in which neighbors must travel along. Follow the figure.

Decision chart with three kinds of file found next to or named like a SQLite database: a database file that starts with the SQLite text, a database with a -wal file beside it, and a file with other first bytes that the engines refuse, with the result for every kind in our tests.
Figure 1. Three kinds of file around a SQLite database, found by the first bytes and the files beside it.

The kinds in detail

Three kinds of file around a SQLite database and how two engines handled them
Kind Files and results
A SQLite database file 20 of 20 files. The sqlite3 shell and pysqlite3 opened all 20, and the integrity check said ok on all 20.
A database with a -wal file 1 of 20 files. own05_wal.db is 4,096 bytes and its -wal file is 127,752 bytes. With both, the people table has 501 rows. With the database alone, the shell lists no tables.
A file the engines refuse 2 of 21 damaged copies. Copy A1 changes the first bytes to XQLite, and copy A4 zeroes page 1. Both engines said file is not a database.

Check the first bytes

The file command named our example a SQLite 3.x database. It also printed the last writer version, the page count and the change counter. So it is a good first clue. Header and pages need more than a label, as the check part shows.

First bytes and header values in 20 files
Value What we measured
First 16 bytes The word SQLite, a space, the text format 3 and a zero byte in 20 of 20 files.
Page size 4,096 in 14 files, 1,024 in 4, 512 in 1 and 65,536 in 1. The header stores 65,536 as the value 1.
Text encoding UTF-8 in 17 files and UTF-16le in 1. Two files show 0, because they have no tables on disk yet.
Write and read versions The value 1 in 19 files and 2 in 1 file, the one in WAL mode.
File size 2 files are under 10,000 bytes, 8 are 10,000 to 99,999, 7 are 100,000 to 999,999 and 3 are 1,000,000 bytes or more.
First bytes first The word SQLite names the file. Any other start was refused in both engines.
The -wal file second Look for a file with the same name plus -wal beside the database.
The page size third Bytes 16 and 17 give it. It must be a power of two from 512 to 65,536.
The ending last Ours end in .db or .sqlite. The ending does not prove the contents.

Is it a different database format? The ESEDB file overview covers the Extensible Storage Engine database.

What Is Inside a SQLite File? A 100-byte header and equal-size pages

SQLite files are series of equal-size pages. Our example is 258,048 bytes, which is 63 pages of 4,096 bytes. Its first 100 bytes are the header, and page 1 lists every table and index.

We looked inside one SQLite file because its layout explains every result later on this page. We made our own01_base.db with Python. It has two tables, people with 2,000 rows and orders with 4,000, and two indexes. The first bytes show the header.

The start of one SQLite file
xxd -l 48 own01_base.db
00000000: 5351 4c69 7465 2066 6f72 6d61 7420 3300  SQLite format 3.
00000010: 1000 0101 0040 2020 0000 0005 0000 003f  .....@  .......?
00000020: 0000 0000 0000 0000 0000 0004 0000 0004  ................

It starts with 53 51 4c 69 74 65, the word SQLite. Two bytes at offset 16 are 10 00, which is 4,096. Four bytes at offset 28 are 00 00 00 3f, which is 63 pages. The shell reports the same facts.

The same values from the sqlite3 shell
sqlite3 own01_base.db "PRAGMA page_size; PRAGMA page_count; PRAGMA encoding; PRAGMA integrity_check;"
4096
63
UTF-8
ok
Diagram of one SQLite database file of 258,048 bytes in six parts: the 100-byte header, page 1 with the schema, table pages, index pages, the files beside it and the bytes inside one page, with three rules about the header, the pages and the sidecar files.
Figure 2. One SQLite file made by us, own01_base.db. The parts are not drawn to scale.

The pages, one by one

SQLite files store everything in pages. Page 1 stores the header and a list of tables and indexes. Every table and index has a root page. In our file people has root page 2, orders has page 3 and the two indexes have pages 4 and 5.

The first byte of a page names its kind. In our file 42 pages start with 0D and 17 start with 0A, and those are leaf pages that store rows. 2 start with 05 and 2 start with 02, and those are interior pages. The dbstat table in the shell agrees, with 59 leaf pages and 4 interior pages.

What every part stores

Parts of a SQLite file and what we saw
Part What it stores
Header The first 100 bytes. Copies A1 and A4 damage the first bytes, and both engines refused them. Our repair script restores A1.
Page count Bytes 28 to 31. Copy B1 says 10 pages and B2 says 5,000. The engines opened B1 with errors and refused B2, and our repair script restored both.
Schema cookie Bytes 40 to 43. Copy B3 changes it to 57,005. Both engines gave the original answer.
Page 1 The list of tables and indexes. Copy A4 zeroes it, and both engines refused the copy.
Table pages The rows. Copy C1 zeroes the root page of people. Both engines reported errors, and a dump saved nothing.
Index pages Copies C2 and C4 damage index pages. Both engines reported errors, and a dump rebuilt every table row.
Files beside it The -wal file stores recent changes. Copy D2 has no -wal file, and the shell lists no tables.

What the public pages say about the layout

The sqlite.org file format page says the header is 100 bytes and page sizes are powers of two from 512 to 65,536. It also says the value 1 at offset 16 means 65,536, and our file with that page size shows 1.

Per that page, SQLite files use a rollback journal or a WAL, never both at once. Header bytes 18 and 19 show which, with 1 for a journal and 2 for a WAL. Our files show 1 in 19 cases and 2 in the WAL file.

The map is small. Our script checks the header only. It examines the first bytes, page size, page count, encoding and freelist pointer. It never looks inside a page, so it printed OK for all seven copies with page damage.

What Did We Find in 20 Test SQLite Files? Two engines, and all 20 files opened in both

We tested 20 SQLite files with two engines. Both opened all 20, and the integrity check said ok on all 20. Both engines gave the same table list and row counts on every file. 12 of the files are ours.

We counted a file as opened when an engine gave an integrity check of ok and then returned the rows of every table. The shell used PRAGMA integrity_check and SELECT count(*) on every table. pysqlite3 used the same check and loaded every row. We used SQLite 3.45.1 and 3.51.1 on Linux.

Bar chart of 20 test SQLite databases by size, from 4,096 bytes to 24,702,976, next to the number of files that two SQLite engines opened, and four measured facts.
Figure 3. 20 databases, two engines and four measured facts.
20 test files
20 opened in both engines
6 with a page size other than 4,096
0 refused by either engine

What the numbers show

Findings from 20 test files
Finding What we measured
Every file opens 20 of 20 files opened in both engines. The two engines gave the same table list and row counts on all 20.
Page sizes vary 14 files use 4,096 bytes, 4 use 1,024, 1 uses 512 and 1 uses 65,536. Both engines opened all of them.
The largest file is big northwind.db is 24,702,976 bytes in 6,031 pages. Its Order Details table has 609,283 rows, and both engines counted them all.
Spatial files open Four files from Datasette and GDAL have a table named geometry_columns. Both engines opened them as ordinary tables, and we did not test any spatial function.
Four files show a zero at byte 96 The two sqlite-jdbc files and two GDAL files show 0 where newer files show the version of the last writer. We did not find why, and both engines opened all four.
One file uses UTF-16 own04_utf16.db stores text in UTF-16le. Both engines opened it and counted 600 people.
One file needs its -wal file own05_wal.db shows 501 people with its -wal file beside it. Without the -wal file it shows no tables.

What this study cannot show you

These are sample databases and test files, and we made 12 of them. Databases from apps contain messy data, many tables and years of edits. A file that passes here shows that the engines accept its layout. It says nothing about the data.

We opened no SQLite file in a desktop app. Output from an engine does not mean that an app can use the data. We compared table lists and row counts on these 20 files, and we did not compare every value in them.

What Happens When a SQLite File Is Damaged? 21 broken copies, and one passed the check with a changed value

We damaged one SQLite file in 21 ways. Both engines gave the same answer on all 21 copies, with 3 same, 11 different and 7 refused. The integrity check said ok on 4 copies, and one of those, C3, had a changed value.

Our base file is own01_base.db, 258,048 bytes. We changed one thing at a time and saved 21 copies. Same means the integrity check said ok and the row counts and a hash of every row matched the original. Refused means the engine stopped with an error. Differs means it gave another answer.

Grid of 21 damaged copies of one SQLite database against two SQLite engines, our check script, a header repair script and a dump salvage. Both engines gave one answer on every copy, the integrity check passed 4 copies and one of those had a changed value.
Figure 4. 21 damaged copies, two engines, our script and two repairs.

Both engines come from the same project, so they agree on every damaged SQLite file. The split we found is another one. A check can say ok about a file whose data changed, and a header repair cannot fix a damaged page.

What the grid shows

Results for 21 damaged copies of one SQLite file
Copies What happened
Wrong first bytes Copy A1 changes the first bytes to XQLite, and copy A4 zeroes page 1. Both engines said file is not a database. The repair script restored A1 and could not restore A4.
A cut file Copy A2 cuts the file in half, and both engines refused it. Copy A3 cuts the last 100 bytes, and both engines reported integrity errors.
A text mode transfer Copy A5 changes every LF into CRLF, which adds 3,124 bytes. Both engines reported integrity errors, and the repair script could not fix it.
Wrong header numbers Copy B1 gives a page count of 10 and B2 gives 5,000. Copy B4 sets the encoding to 7, B7 sets the version bytes to 3 and A6 gives a bad page size. Both engines refused all but B1. The repair script restored B1, B2 and B7.
Changes that did not matter Copy A7 adds 777 bytes at the end, B3 changes the schema cookie and B6 changes the writer version. Both engines gave the original answer on all three.
A wrong freelist pointer Copy B5 points the freelist at page 9,999. Both engines reported an error. The repair script clears the pointer, and the copy then read as the original.
A damaged page Copies C1 to C7 zero root pages, zero part of one page, swap two pages, copy one page over its neighbor or reset a page header. Both engines reported errors on all but C3.

The pattern is clear. Header damage is easiest to fix, because the repair needs only the first 100 bytes. Page damage is harder, because the rows on it are gone. Index damage is the exception, because a dump builds the index again from its table.

The silent byte flip

Copy C3 flips one byte inside a page of the orders table. The integrity check said ok in both engines, and every row count matched. A dump of the copy showed the change. Order 572 went from a price of 26.6 to a price of 26.4007812500000014.

The check looks at page shapes. It does not know what a price should be. Only a known-good copy or a stored hash shows a change like this one. Keep hashes of files that matter, and compare them before you trust the data.

Our script and the repairs

Our check script printed OK for 9 copies and WARN or FLAG for 12. Two of the 9 OK copies, B3 and B6, read as the original. The other 7, C1 to C7, did not, because the script checks the header only. See the script.

The repair script of 14 lines restored 5 copies, A1, B1, B2, B5 and B7. It changes only the first 100 bytes. It cannot restore a cut file, a changed page or a text mode copy. We did not test a repaired file in an app.

The dump salvage is the command sqlite3 broken.db .dump | sqlite3 saved.db. It saved every table row in B5, C2 and C4. For C3, C5 and C6 it saved rows that differ from the original. For 12 copies it saved nothing. We did not test .recover, because the shell in our lab lacks the table that it needs.

Why a text mode copy is special

Copy A5 changes LF into CRLF, and a text mode transfer does the same. Our base file has 3,124 LF bytes and 16 CRLF pairs. Replacing every CRLF pair in A5 with LF gave back the original file, byte for byte, and both engines then gave the original answer.

Do not apply that replacement to a good file. 12 of our 20 files contain a CRLF pair by chance, and the replacement would damage our base file by 16 bytes. So reverse a text mode copy only after you know the transfer made it. A new copy in binary mode is the better fix.

What Does Our Expert Say About SQLite Files? A note from Neeraj Singh on trusting one check

We asked our expert what the grid means for someone who has a SQLite file and has to decide if it is damaged. He looked at the 21 copies and the check results.

Neeraj Singh

Expert insight

Copy the file before you open it, and copy the -wal file with it. Then keep a hash of a known-good copy. In our tests the integrity check passed a copy with one changed price, and only a comparison with the original showed it.

Neeraj Singh Manager, Tech Support & Operations 19+ years fixing Windows and system errors

That advice fits the lab. The integrity check said ok on 4 copies, and a row hash caught the one that changed. Opening our WAL example merged its -wal file into the database and removed that file. A first copy keeps the original pair.

How Do You Open a SQLite File? The shell and Python, with the commands we tested

To open a SQLite file, use the sqlite3 shell or Python. Both opened all 20 of our SQLite files. Copy the file first, because opening a WAL database can merge its -wal file into it. An engine that opens a file does not prove the file is sound.

Programs with windows exist too. The sqlitebrowser project on GitHub makes a free desktop app, and FileInfo lists more. Our lab uses Linux and has no desktop app, so we tested none. The tools below are the ones we used.

Your options

sqlite3 shell Tested The command line tool from the SQLite project. Version 3.45.1 opened all 20 files and listed the tables with the .tables command.
Python Tested The sqlite3 module and pysqlite3 with SQLite 3.51.1 opened all 20 files and returned every row.
DB Browser for SQLite Not tested A desktop app from the sqlitebrowser project on GitHub. FileInfo lists it as SQLite Database Browser.
The file command Tested It names the file and prints the page count and the last writer. It opens nothing.
Other programs Not tested FileInfo lists more programs that open SQLite files. We did not use them.

Copy first, then look

Copy the database and any -wal file, and work on the copies. In our WAL example the database is 4,096 bytes and its -wal file is 127,752 bytes. After one shell query the database was 86,016 bytes and the -wal file was gone.

Opening a WAL database merges its -wal file
ls -l own05_wal.db*
4096 own05_wal.db
127752 own05_wal.db-wal
sqlite3 own05_wal.db "SELECT count(*) FROM people;"
501
ls -l own05_wal.db*
86016 own05_wal.db

The WAL page on sqlite.org explains this. When the last connection closes, SQLite makes a final checkpoint and deletes the -wal and -shm files. The same page says the -wal file is part of the database, so copy the two together.

Look at the tables

Start with table names and row counts. Both commands below change nothing in the file. The shell lists our two tables in sorted order, and Python lists them in schema order.

The tables of one SQLite file
sqlite3 own01_base.db ".tables"
orders  people
python3 -c "import sqlite3; c = sqlite3.connect('own01_base.db'); print([r[0] for r in c.execute(\"select name from sqlite_master where type='table'\")])"
['people', 'orders']
Copy the pair first Opening a WAL database merged our -wal file and removed it.
Look at the tables Table names and row counts show if the data is there.
Use a second engine We used two engines, and they agreed on all 20 files and all 21 copies.
Know the limit An engine can say ok about a file with a changed value, as copy C3 shows.

Is the file an Access database? The ACCDB file overview covers the newer Access format.

How Do You Export a SQLite File to SQL or CSV? Four commands, tested on one file

To export a SQLite file, use .dump for SQL and the CSV mode for one table. Our example made a SQL file of 385,935 bytes. Loading it gave back every row, and a dump of the new file matched the old dump byte for byte.

Both exports leave the original alone. We checked the results with the shell and with cmp. SQL suits a full copy, because it stores the schema and every table. CSV stores one table of values.

Export it to SQL

The .dump command prints SQL text that rebuilds the database. Our dump of own01_base.db is 385,935 bytes, and gzip makes it 58,446 bytes. We loaded the text into a new file and compared the two dumps with cmp. They matched.

A SQL dump and a round trip
sqlite3 own01_base.db .dump > own01.sql
wc -c own01.sql
385935 own01.sql
sqlite3 new.db < own01.sql
sqlite3 new.db .dump | cmp - own01.sql && echo same
same

Export one table to CSV

The CSV mode prints values with commas. The -header option adds a row of column names. The full people table of our example has 2,000 rows and 52,782 bytes. In our output Person 0 is in quotes and Leeds is not.

Three rows of one table as CSV
sqlite3 -header -csv own01_base.db "SELECT id,name,city,age FROM people LIMIT 3;"
id,name,city,age
1,"Person 0",Leeds,20
2,"Person 1",Pune,21
3,"Person 2",Lyon,22

Make a fresh copy

Two commands make a new SQLite file from a live one. VACUUM INTO and .backup both made 258,048 bytes from our example, and both dumps matched the original dump. The .backup copy differs from the original in 3 header bytes. The VACUUM INTO copy differs in 199,557 bytes, because it rebuilds pages.

The sqlite.org page on corruption warns that copying a file during a transaction can mix old and new content. It names the backup API, VACUUM INTO and sqlite3_rsync as ways to make a correct copy.

What we checked after the exports
Export Result
SQL dump 385,935 bytes. A new file made from it gave the same dump, byte for byte, and 2,000 people and 4,000 orders.
CSV of people 52,782 bytes and 2,000 lines, one for every row of the people table.
VACUUM INTO 258,048 bytes. The integrity check said ok, and the dump matched the original.
.backup 258,048 bytes. Only 3 header bytes differ, and the dump matched the original.

What we did not test. We exported one small file. We did not test dumps of large SQLite files, files with views and triggers or files with blob columns. We make no claim about those.

Do you need the spreadsheet side of the data? The XLS file overview covers the Excel 97 to 2003 workbook.

How Do You Check a SQLite File? Six steps and a script of 22 lines

Check a SQLite file in six steps. Copy it first. Then look at its first bytes, ask the engine for an integrity check, list tables, compare row counts and compare a hash with a known-good copy. Our script of 22 lines checks the header.

  1. Copy the SQLite file and any -wal file, and work on the copies. Keep the originals.
  2. Look at the first bytes with xxd -l 16 file.db. They must read 53 51 4c 69 74 65, the word SQLite.
  3. Ask the shell for an integrity check with sqlite3 file.db "PRAGMA integrity_check;". A good file prints ok.
  4. List the tables with .tables and count the rows of every table with SELECT count(*).
  5. Compare sqlite3 file.db .dump | sha256sum with the value from a known-good copy. Only this step caught copy C3.
  6. Open the file in a second engine, and compare the table list and the counts.

The check in Python

Our script of 22 lines checks the header with the standard library. It prints OK, WARN or FLAG with the problem it found. It never looks inside a page, so it cannot see a damaged page or a changed value.

sqlite_peek.py, 22 lines
import sys,os,struct
def peek(fp):
    d=open(fp,'rb').read(); n=len(d); out=[]
    if n<100: return [('FLAG','file is %d bytes, header needs 100'%n)]
    if d[:16]!=b'SQLite format 3\0': out.append(('FLAG','magic text is %r'%d[:16]))
    ps=struct.unpack('>H',d[16:18])[0]; ps=65536 if ps==1 else ps
    if ps<512 or ps>65536 or ps&(ps-1): out.append(('FLAG','page size %d is not a power of two from 512 to 65536'%ps)); ps=None
    if d[18] not in (1,2) or d[19] not in (1,2): out.append(('FLAG','file format versions %d/%d'%(d[18],d[19])))
    enc=struct.unpack('>I',d[56:60])[0]
    if enc not in (0,1,2,3): out.append(('FLAG','text encoding %d'%enc))
    if ps:
        if n%ps: out.append(('WARN','size %d is not a multiple of page size %d'%(n,ps)))
        pc=struct.unpack('>I',d[28:32])[0]; cc=struct.unpack('>I',d[24:28])[0]; vv=struct.unpack('>I',d[92:96])[0]
        if pc and vv==cc and pc!=n//ps: out.append(('FLAG','header says %d pages, file has %d'%(pc,n//ps)))
        ft=struct.unpack('>I',d[32:36])[0]
        if ft>n//ps: out.append(('FLAG','freelist trunk page %d is past the end'%ft))
    if os.path.exists(fp+'-wal'): out.append(('WARN','a -wal file is beside it (%d bytes) and must travel with it'%os.path.getsize(fp+'-wal')))
    return out or [('OK','header looks sane')]
if __name__=='__main__':
    for f in sys.argv[1:]:
        r=peek(f); worst='FLAG' if any(k=='FLAG' for k,_ in r) else 'WARN' if any(k=='WARN' for k,_ in r) else 'OK'
        print(os.path.basename(f),worst,'; '.join(m for _,m in r)[:120])

What the script prints

First comes a good file. Next is copy A1, which starts with XQLite. Then copy B1, which says 10 pages in a file of 63. Then comes copy C3, with the changed price. Last is the WAL example with its -wal file beside it.

Five examples of the check script
python3 sqlite_peek.py own01_base.db
own01_base.db OK header looks sane
python3 sqlite_peek.py A1.db
A1.db FLAG magic text is b'XQLite format 3\x00'
python3 sqlite_peek.py B1.db
B1.db FLAG header says 10 pages, file has 63
python3 sqlite_peek.py C3.db
C3.db OK header looks sane
python3 sqlite_peek.py own05_wal.db
own05_wal.db WARN a -wal file is beside it (127752 bytes) and must travel with it

The repair script

The repair script of 14 lines changes the first 100 bytes only. It restores first bytes when the rest of the text is there, sets the page count from file size and clears a freelist pointer past the end. It restored 5 of our 21 damaged copies.

sqlite_fix.py repairs a damaged header
python3 sqlite_fix.py A1.db A1_fixed.db
A1.db -> A1_fixed.db magic restored
sqlite3 A1_fixed.db "PRAGMA integrity_check;"
ok
python3 sqlite_fix.py B1.db B1_fixed.db
B1.db -> B1_fixed.db page count set to 63
sqlite3 B1_fixed.db "SELECT count(*) FROM people; SELECT count(*) FROM orders;"
2000
4000

Our base file has no free pages, so clearing the freelist pointer lost nothing. On a file with free pages it would leave them unused. The script cannot guess a lost page size or an encoding, and it set the version bytes to 1 on A4 and B7. The guess matched our base file, and only B7 then opened.

Know the limits. The script printed OK for seven copies where we damaged a page, and the integrity check said ok for copy C3 with a changed price. So an OK from one script is only a start, and a hash compared with a known-good copy is the test.

Is the file a ZIP archive and not a database? The ZIP file overview covers how to test one.

Is a SQLite File Safe to Open? Listing the tables is safe, and opening needs care

Listing the tables of a SQLite file starts nothing. Our 20 SQLite files stored tables, indexes, views and triggers, and nothing else. A file from an unknown source still needs care, so check what it is before you open it in an app.

Four of our 20 files have triggers, 148 in all, and five have views, 30 in all. A trigger is SQL text stored in the file, and SQLite starts it when a row changes. We looked at the names of these parts, and we did not test one that does anything harmful.

The Library of Congress page says a text editor shows the header and schema text, while binary structures hide most data. So a quick look at first bytes and schema shows more than the ending does.

Our lab only looked at SQLite files. We used the shell, pysqlite3 and our own scripts on 20 files, and we opened none in a desktop app. We scanned no file for malware, so we make no claim about the files in our set or about yours.

Listing the tables is safe The .tables command and our check script show names and start nothing. We used them on 20 files.
The file can store SQL Views and triggers are SQL text in the schema. Our own12_view_trig.db has one view and one trigger, and the four public files with views or triggers opened without a problem.
We found no extra parts The schema of every file lists only tables, indexes, views and triggers.
The ending is a label A file named .db can contain anything. Look at the first bytes before you trust the ending.
An encrypted file looks different The Library of Congress says the public format has no encryption, and an add-on named SEE encrypts the entire file. We tested no encrypted file.
Work on a copy Our scripts leave the source alone. Opening a WAL database changes it, so copy first.

Five steps for a file you do not know

  1. Get the file from a source you trust, and keep the original.
  2. Look at the first bytes. A SQLite file starts with 53 51 4c 69 74 65, and anything else is another kind of file.
  3. List the schema with sqlite3 file.db .schema. Look for views and triggers that you did not expect.
  4. Open a copy in an updated tool, and keep the tool up to date.
  5. Export the tables to CSV if you only want the values, and look at that file instead.

What we did not test. We did not scan for malware, test harmful triggers or open SQLite files in desktop apps. These checks lower the chance of a surprise, and they do not remove it.

Why Will Your SQLite File Not Open? Seven causes, and what to do about every one

Seven causes stop a SQLite file from opening or make it look empty, and the file is the cause in only some of them. Start with the first bytes. A file that does not start with the word SQLite is refused by every engine we used.

Work down the list in order. Cause five comes from our WAL example and the sqlite.org WAL page. Cause seven comes from the Library of Congress. Causes one to four and six come from our 21 damaged copies and 20 files, so every fix names a copy or a count.

1. The file is not a SQLite file Copies A1 and A4 start with other bytes, and both engines said file is not a database. Ask the sender for the file again, and ask what program made it.
2. A text mode transfer changed it Copy A5 changes every LF into CRLF, and both engines reported integrity errors. Download the file again in binary mode. Reverse the change only if you know the transfer made it.
3. The file is cut short Copy A2 is cut in half, and both engines refused it. Copy A3 loses its last 100 bytes, and both reported errors. A dump saved nothing from either, so get a new copy.
4. A header number is wrong Copies A6, B1, B2, B4 and B7 give bad page sizes, bad page counts, bad encodings or bad version bytes. Our repair script restored B1, B2 and B7. It cannot guess a lost page size or encoding.
5. The -wal file is missing Our WAL database alone shows no tables, and with its -wal file it shows 501 people. Find the -wal file that went with the database and put it beside the file with the same name.
6. A page is damaged Copies C1 to C7 damage pages, and both engines reported errors on six. A dump saved every row when only an index was hurt, as in C2 and C4. Try .dump first, then get a new copy.
7. The file is encrypted The Library of Congress says an add-on named SEE encrypts the entire file. Such a file would not start with the SQLite text. We tested no encrypted file, so ask the owner of the file.

How Does a SQLite File Differ From a SQL Dump and a CSV File? One database, three ways to store it

SQLite files are binary databases made of pages. A SQL dump is text that rebuilds the database, and a CSV file is text for one table. We exported the same data both ways and compared the sizes and the results.

The table lists the neighbors of a SQLite file. We tested the three rows for SQLite, SQL and CSV with one example file. The Access row is a pointer, because we did not test Access files.

A SQLite file and its neighbors compared
Type What separates it
SQLite file Binary pages behind a 100-byte header. 258,048 bytes for our example, with 2 tables and 2 indexes. An engine opens it directly.
SQL dump Text made by the .dump command. 385,935 bytes for the same data, or 58,446 bytes after gzip. Loading it rebuilds the file.
CSV file Text for one table. 52,782 bytes for the people table. It has no indexes and no second table.
Access database The MDB and ACCDB files from Microsoft Access are other formats. We did not test them for this page.

SQLite or a SQL dump

Dumps suit sending databases as text, because any editor opens them. We loaded our dump into a new file, and a dump of that file matched the old dump byte for byte. The SQL file is bigger, at 385,935 bytes against 258,048.

SQLite or CSV

CSV suits spreadsheets, because it stores values only. A CSV file cannot rebuild the database. Our people table made 2,000 lines, and the orders table, indexes and schema stay behind in the SQLite file.

What Is the SQLite File Format Made Of? The facts we could check, and where they came from

A SQLite file stores a 100-byte header and equal-size pages in one disk file. Facts below come from our checks of 20 files and 21 damaged copies. Sources are Wikipedia, the Library of Congress, sqlite.org and the test tools.

Facts about SQLite files
Part What we could check
Maker D. Richard Hipp, with version 1.0 in August 2000, per Wikipedia.
Name and endings SQLite format 3. Wikipedia lists the endings .sqlite, .sqlite3, .db, .db3, .s3db and .sl3. The Library of Congress says the format defines no official ending.
Status The Library of Congress calls the format openly documented, with the specification and code in the public domain.
Container One disk file of equal-size pages. The page size is a power of two from 512 to 65,536 bytes, per sqlite.org. Ours are 512, 1,024, 4,096 and 65,536.
First bytes The text SQLite format 3 and a zero byte, per the Library of Congress. All 20 of our files agree.
Header 100 bytes. Offset 16 gives the page size, offsets 18 and 19 the journal or WAL versions, offset 28 the page count and offset 56 the text encoding, per sqlite.org. We confirmed all four in our files.
Files beside it A -wal file and a -shm file in WAL mode, and a -journal file for a rollback journal, per sqlite.org. We tested the -wal file only.
Media type application/vnd.sqlite3, registered in 2018, per the Library of Congress.
Encryption The public format has none, per the Library of Congress. An add-on named SEE encrypts the entire file. We tested none.
Viewers FileInfo lists the sqlite3 shell, a browser app and others. We tested the shell and Python.

About the values we list

Layout facts come from our own tests of 20 files and from the pages we name. The Wikipedia article is the source for history and endings. The Library of Congress record is the source for first bytes, media type and the encryption note.

The file format document is the source for header and page sizes. We also used the WAL page and the page on corruption. We could not load the last 6,000 characters of that file format page, so we make no claim from them.

What we left out

We used no Windows machine and no desktop app. We did not test encrypted SQLite files, hot journals, files over 25 MB, harmful triggers or repairs with .recover. Our engines worked on Linux, and other software can accept or refuse files that they treat differently.

The 8 public SQLite files come from five projects, and we made 12 files with Python. Our damaged copies come from one small file and change one thing at a time. We compared row counts and a hash of the rows, and we did not compare every file in the set.

Sources checked

For the format and its history, see the SQLite article on Wikipedia and the Library of Congress format description. For the file layout, see the database file format spec. For viewers, see FileInfo.

Public test files come from the Chinook database, Northwind port, Datasette source, GDAL source and sqlite-jdbc source. We did not test the desktop app from the sqlitebrowser project.

Frequently Asked Questions About SQLite Files

What is a SQLite file?

A SQLite file is a database in a single file, made by the SQLite engine. It stores tables, indexes, triggers and views in equal-size pages. The file starts with the text SQLite format 3, and the same file works on every platform, per the Library of Congress.

Who made SQLite?

D. Richard Hipp designed SQLite in the spring of 2000 and released version 1.0 in August 2000, per Wikipedia. The code is in the public domain. The format is openly documented, and the Library of Congress keeps a format record for it.

How do I open a SQLite file?

Use the sqlite3 shell or Python. Both opened all 20 of our files. Copy the file first, and copy any -wal file with it. Desktop apps exist too, and FileInfo lists some of them. We tested none, because our lab uses Linux.

What is a DB file?

The ending .db is a label. Wikipedia lists it among the SQLite endings, and the Library of Congress says the format defines none. A file named .db can contain anything, so check the first bytes. A SQLite file starts with the word SQLite.

How do I convert a SQLite file to CSV?

Use the shell with the CSV mode, for example `sqlite3 -header -csv file.db "SELECT * FROM people;"`. It makes one CSV file for one table. Our people table gave 2,000 lines and 52,782 bytes. The CSV file has no indexes and no other table.

How do I convert a SQLite file to SQL?

Use the `.dump` command in the shell. Our example made a SQL file of 385,935 bytes. We loaded it into a new file, and a dump of the new file matched the old dump byte for byte. gzip made the SQL file 58,446 bytes.

What are the -wal and -shm files?

In WAL mode, the -wal file stores recent changes, and the -shm file is a shared-memory index, per sqlite.org. SQLite deletes both when the last connection closes. Our -wal file was 127,752 bytes, and the database alone showed no tables. Copy the three files together.

Is a SQLite file safe to open?

Listing the tables of one starts nothing. Our 20 SQLite files stored only tables, indexes, views and triggers. A trigger is SQL text that SQLite starts when a row changes, and we did not test a harmful one. We scanned no file for malware. Open files from sources you trust, and work on a copy.

Why will my SQLite file not open?

One cause is a file that does not start with the SQLite text. Others are text mode transfers, cut files, wrong header numbers, missing -wal files and damaged pages. Start with first bytes, then work down the cause list in the part called Why Will Your SQLite File Not Open.

What does file is not a database mean?

Our shell printed it for four damaged copies, A1, A4, A6 and B7. Three had a wrong header, with bad first bytes, a bad page size or bad version bytes. In A4 page 1 was zeroed. Check the first bytes, and compare them with the SQLite text.

Can I repair a damaged SQLite file?

Sometimes. Header repair restored 5 of our 21 damaged SQLite files, and a dump saved every table row in 3. Neither repair restored SQLite files with a damaged table page. We did not test `.recover`. Keep the original, and work on a copy.

Is the integrity check enough?

No. The integrity check said ok on 4 of our 21 damaged copies, and one of them had a changed price. The check looks at page shapes and not at values. Compare a dump hash with the hash of a known-good copy.

Still Stuck With a SQLite File? Where to go next, depending on what went wrong

Your SQLite file will not open

Look at the first bytes and use the check script. The cause list names what to try in order.

You do not know the kind

The kinds table names the kind, and the neighbors name the formats that look alike.

Is it an Access database? The page on the older Access format covers the MDB file.

Is it an archive? The page on ZIP archives covers the compressed archive.

Is it a generic data file? The DAT file overview covers the .dat file type.

Is it a spreadsheet? The page on Excel workbooks covers the Excel 97 to 2003 format.

Is it a file type you do not know? Browse the file extension resource library, or send it on the submit a file type page.

Do you need the format facts? Read the format part for the layout and the sources of SQLite files.