11# sqlitexplorer
22
3- A small command-line explorer for SQLite databases. It lists tables, prints
4- schemas, dumps rows and runs ad-hoc queries, rendering the results as terminal
5- tables with [ outfancy] ( https://github.com/carlosplanchon/outfancy ) .
3+ A command-line explorer for SQLite databases. It lists tables, prints schemas,
4+ dumps rows, computes statistics, searches values, runs ad-hoc queries, draws
5+ charts, exports and imports data, and offers an interactive shell. Tables are
6+ rendered with [ outfancy] ( https://github.com/carlosplanchon/outfancy ) and charts
7+ with [ plotille] ( https://github.com/tammoippen/plotille ) .
68
79## Installation
810
@@ -15,31 +17,132 @@ uv tool install .
1517pip install .
1618```
1719
18- ## Usage
20+ ## Quick tour
1921
2022``` sh
21- sqlitexplorer tables app.db # tables and views with their row counts
22- sqlitexplorer schema app.db # every CREATE statement in the database
23- sqlitexplorer schema app.db users # one table with its indexes and triggers
24- sqlitexplorer describe app.db users # columns: type, NOT NULL, default, primary key
25- sqlitexplorer show app.db users # all the rows of a table or view
26- sqlitexplorer show app.db users -n 20 --offset 40
27- sqlitexplorer query app.db " SELECT name, age FROM users WHERE age > 30"
23+ sqlitexplorer tables app.db # tables and views with their row counts
24+ sqlitexplorer schema app.db [users] # CREATE statements, in creation order
25+ sqlitexplorer describe app.db users # columns: type, NOT NULL, default, primary key
26+ sqlitexplorer indexes app.db [users] # indexes and the columns they cover
27+ sqlitexplorer foreign-keys app.db [posts] # foreign keys
28+ sqlitexplorer info app.db [--check] # size, pragmas, object counts, integrity check
29+ sqlitexplorer show app.db users # rows of a table or view
30+ sqlitexplorer stats app.db users # nulls, distinct, min, max, top values per column
31+ sqlitexplorer search app.db " marie" # find a text in every column of every table
32+ sqlitexplorer query app.db " SELECT ..." # run SQL
33+ sqlitexplorer chart app.db " SELECT day, total FROM sales ORDER BY day"
34+ sqlitexplorer dump app.db # the whole database as SQL
35+ sqlitexplorer export app.db users -o users.csv # rows to a file
36+ sqlitexplorer import app.db people people.csv # a file into a table
37+ sqlitexplorer diff app.db backup.db # compare two schemas
38+ sqlitexplorer shell app.db # interactive shell
39+ ```
40+
41+ Every command has ` --help ` . Databases are opened read-only and are never
42+ created; commands that change data need ` --write ` , except ` import ` , which
43+ always writes.
44+
45+ ## Exploring
46+
47+ ` show ` accepts filters so most questions need no SQL:
48+
49+ ``` sh
50+ sqlitexplorer show app.db users -c name,age --where " age > 18" --order-by age --desc -n 20 --offset 40
51+ ```
52+
53+ ` --columns ` and ` --order-by ` are validated against the table; ` --where ` is a
54+ raw SQL condition.
55+
56+ ` stats ` reports, for each column, the declared type, how many NULLs, how many
57+ distinct values, the minimum, the maximum and the most frequent values
58+ (` --top N ` ). ` search ` looks for a case-insensitive substring in every non-BLOB
59+ column of every table (` --table ` restricts it, ` --limit ` stops early) and
60+ prints the table, column, rowid and value of each match.
61+
62+ ## Output options
63+
64+ Every command that prints rows takes the same options:
65+
66+ | Option | Effect |
67+ | ---| ---|
68+ | ` --format ` , ` -f ` | ` table ` (default), ` csv ` , ` tsv ` , ` json ` or ` markdown ` |
69+ | ` --null TEXT ` | text shown for NULL values (default ` NULL ` ) |
70+ | ` --truncate N ` | cut values longer than N characters |
71+ | ` --page N ` , ` --page-size M ` | print only one page of the rows; the footer goes to stderr |
72+ | ` --pager ` | send the output to ` $PAGER ` (default ` less -R ` ) when on a terminal |
73+ | ` --color ` / ` --no-color ` | force or disable ANSI colors; by default only on a terminal, never when ` NO_COLOR ` is set |
74+ | ` --width N ` | width to fit tables into (default: the terminal width) |
75+
76+ ` NULL ` and ` BLOB ` values are printed as SQL literals (` NULL ` , ` X'0102' ` ). In
77+ the JSON format NULL becomes ` null ` , BLOBs are base64 strings and values are
78+ never truncated.
79+
80+ ## Queries
81+
82+ ``` sh
83+ sqlitexplorer query app.db " SELECT name, age FROM users WHERE age > :min" -p min=30
84+ sqlitexplorer query app.db --file report.sql # several statements, one transaction
2885echo " SELECT COUNT(*) FROM users" | sqlitexplorer query app.db -
86+ sqlitexplorer query app.db " SELECT ..." --attach old=backup.db # then use old.users
87+ sqlitexplorer query app.db " SELECT ..." --explain # EXPLAIN QUERY PLAN as a tree
88+ sqlitexplorer query app.db " SELECT ..." --time # rows and elapsed time on stderr
89+ sqlitexplorer query app.db " SELECT COUNT(*) FROM jobs" --watch 2 # re-run every 2 s, Ctrl-C stops
2990sqlitexplorer query app.db " DELETE FROM users WHERE age IS NULL" --write
3091```
3192
32- Databases are opened read-only and are never created. Pass ` --write ` to
33- ` query ` to run statements that modify data; the change is committed when the
34- statement succeeds and rolled back otherwise. ` NULL ` and ` BLOB ` values are
35- printed as SQL literals (` NULL ` , ` X'0102' ` ).
93+ Several statements can be given at once (separated by ` ; ` ); they run in one
94+ transaction that is committed at the end and rolled back on the first error.
95+ ` --param ` values that look like numbers are bound as numbers. Attached
96+ databases follow the read-only rule of the main one.
97+
98+ ## Charts
99+
100+ ``` sh
101+ sqlitexplorer chart app.db " SELECT day, sales, returns FROM daily ORDER BY day"
102+ sqlitexplorer chart app.db " SELECT age FROM users" --kind hist --bins 20
103+ ```
104+
105+ The first column is the X axis (numbers or ISO dates), every other column is a
106+ series named after the column; rows with NULLs are skipped and counted on
107+ stderr. ` --kind ` selects ` line ` (default), ` scatter ` or ` hist ` (first column
108+ only, ` --bins ` ). ` --height ` , ` --width ` , ` --x-label ` , ` --y-label ` and ` --color `
109+ adjust the drawing.
110+
111+ ## Export, import, dump and diff
112+
113+ ``` sh
114+ sqlitexplorer dump app.db -o backup.sql # replayable SQL, like .dump
115+ sqlitexplorer export app.db users -f json -o users.json
116+ sqlitexplorer export app.db --all -o exported/ -f csv # one file per table and view
117+ sqlitexplorer import app.db people people.csv # csv, tsv or json (list of objects)
118+ sqlitexplorer diff app.db backup.db # exit status 1 when the schemas differ
119+ ```
120+
121+ ` import ` creates the table when it does not exist, inferring INTEGER, REAL or
122+ TEXT for each column; empty CSV cells become NULL. The format comes from the
123+ file extension unless ` --format ` is given.
124+
125+ ## Shell
126+
127+ ```
128+ $ sqlitexplorer shell app.db
129+ sqlitexplorer> SELECT name
130+ ...> FROM users;
131+ sqlitexplorer> .tables
132+ sqlitexplorer> .format json
133+ sqlitexplorer> .quit
134+ ```
36135
37- Tables adapt to the terminal width. Use ` --width N ` to render for another width
38- (for example when redirecting the output to a file) and ` --color ` /
39- ` --no-color ` to override the automatic color detection (` NO_COLOR ` is
40- honoured).
136+ Statements end with ` ; ` and may span several lines. Dot-commands: ` .tables ` ,
137+ ` .schema [NAME] ` , ` .describe TABLE ` , ` .indexes [TABLE] ` , ` .stats TABLE ` ,
138+ ` .format FORMAT ` , ` .null TEXT ` , ` .truncate N|off ` , ` .help ` and ` .quit ` . Tab
139+ completes SQL keywords, table and column names; the history is kept in
140+ ` $XDG_STATE_HOME/sqlitexplorer/history ` (` ~/.local/state ` by default). With
141+ ` --write ` every statement is committed as soon as it succeeds. SQL can also be
142+ piped into the shell.
41143
42- Run ` sqlitexplorer --help ` or ` sqlitexplorer <command> --help ` for every option.
144+ Completion of the command line itself (commands, options and table names) is
145+ installed with ` sqlitexplorer --install-completion ` .
43146
44147## Development
45148
0 commit comments