From 7e4a02e66fda560e5d6a85580da02a8cbacb4390 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 29 Aug 2026 21:05:44 -0500 Subject: [PATCH] Add Output Query Result In Nicely Formatted Table as a SQLite TIL --- README.md | 3 +- ...-query-result-in-nicely-formatted-table.md | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 sqlite/output-query-result-in-nicely-formatted-table.md diff --git a/README.md b/README.md index f653640..aaf1c10 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/sqlite/output-query-result-in-nicely-formatted-table.md b/sqlite/output-query-result-in-nicely-formatted-table.md new file mode 100644 index 0000000..d776f98 --- /dev/null +++ b/sqlite/output-query-result-in-nicely-formatted-table.md @@ -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.