Add Output Query Result In Nicely Formatted Table as a SQLite TIL

This commit is contained in:
jbranchaud
2026-08-29 21:05:44 -05:00
parent 965bade9d4
commit 7e4a02e66f
2 changed files with 52 additions and 1 deletions
+2 -1
View File
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
_1875 TILs and counting..._
_1876 TILs and counting..._
See some of the other learning resources I work on:
@@ -1656,6 +1656,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Display Results In Readable Column Format](sqlite/display-results-in-readable-column-format.md)
- [Explore The Database Schema](sqlite/explore-the-database-schema.md)
- [Manage Lightweight Schema Migrations With `user_version`](sqlite/manage-lightweight-schema-migrations-with-user-version.md)
- [Output Query Result In Nicely Formatted Table](sqlite/output-query-result-in-nicely-formatted-table.md)
### Streaming
@@ -0,0 +1,50 @@
# Output Query Result In Nicely Formatted Table
When I start a fresh SQLite connection and run a query, all the results are
squished together in a way that is poorly formatted, hard to read, and missing
column headers.
```sql
sqlite> select sessions.id, start_time, end_time, projects.name from sessions join projects on projects.id = sessions.project_id limit 3;
1|2026-07-26T21:15:50.062936+00:00|2026-07-26T21:53:13.990Z|taco
2|2026-07-26T21:53:40.019946+00:00|2026-07-26T22:14:09.168Z|TIL
3|2026-08-02T17:00:16.119169+00:00|2026-08-02T17:30:16.119Z|py-vmt
```
I can drastically improve the look of this by turning _headers_ on and switching
to _box_ mode.
```sql
sqlite> .headers on
sqlite> .mode box
sqlite> select sessions.id, start_time, end_time, projects.name from sessions join projects on projects.id = sessions.project_id limit 3;
id start_time end_time name
1 2026-07-26T21:15:50.062936+00:00 2026-07-26T21:53:13.990Z taco
2 2026-07-26T21:53:40.019946+00:00 2026-07-26T22:14:09.168Z TIL
3 2026-08-02T17:00:16.119169+00:00 2026-08-02T17:30:16.119Z py-vmt
```
I personally find that much easier on the eyes. It is also a nicer format to
copy and paste into a post like this or a formatted code block that I'm sharing
with a colleague.
I can also do this directly from the CLI with a one-liner using `-header` and
`-box` like so:
```bash
sqlite3 /Users/lastword/.local/share/vmt/sessions.db -header -box "select sessions.id, start_time, end_time, projects.name from sessions join projects on projects.id = sessions.project_id limit 3"
┌────┬──────────────────────────────────┬──────────────────────────┬────────┐
│ id │ start_time │ end_time │ name │
├────┼──────────────────────────────────┼──────────────────────────┼────────┤
1 │ 2026-07-26T21:15:50.062936+00:00 │ 2026-07-26T21:53:13.990Z │ taco │
2 │ 2026-07-26T21:53:40.019946+00:00 │ 2026-07-26T22:14:09.168Z │ TIL │
3 │ 2026-08-02T17:00:16.119169+00:00 │ 2026-08-02T17:30:16.119Z │ py-vmt │
└────┴──────────────────────────────────┴──────────────────────────┴────────┘
```
Run the `.help` dot-command from a SQLite prompt for a full listing of these
commands. See also `sqlite3 --help` from the CLI for usage details about all
flags.