loganhasson loganhasson - 1 month ago 9
SQL Question

SQLite3 Terminal Output Formatting .width

This is just purely for eye candy while working with SQLite in the terminal, but is there a way to format column width with headers so that each header is resized appropriately (and independently of the other columns)? In other words, here's the output with

.width auto


for a simple table

Id Name Price
---------- ---------- ----------
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600


It does what I'd expect. It resizes each column so that the longest item in any one column can be displayed. However, I'd like to automatically have the output formatted such that each column is wide enough for the longest item in only its column. In other words, I don't want to have to type in

.width 2 10 5


after the fact to get this output

Id Name Price
-- ---------- -----
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 35000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600


Is there something I can do to automate column sizing correctly?

CL. CL.
Answer

The sqlite3 tool has no such function.

You would have to compute the column widths by hand (SELECT max(length(col1)) ...).