Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 955
loganhasson Avatar asked Sep 19 '13 18:09

loganhasson


People also ask

Which of the following commands are used to format the output in SQLite?

The sqlite3 program is able to show the results of a query in eight different formats: "csv", "column", "html", "insert", "line", "list", "tabs", and "tcl". You can use the ". mode" dot command to switch between these output formats.

How do I enable column headers in SQLite?

Pretty simple really. Therefore, to enable column headers, simply use . headers on . As mentioned, you can disable column headers using .

How do I get out of SQLite command-line?

Terminate the sqlite3 program by typing your system End-Of-File character (usually a Control-D). Use the interrupt character (usually a Control-C) to stop a long-running SQL statement.


2 Answers

The sqlite3 tool has no such function.

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

like image 77
CL. Avatar answered Sep 25 '22 08:09

CL.


For "human readable" output, you can use column mode, and turn header output on. That will get you something similar to the sqlplus output in your examples:

sqlite> select * from foo;
234|kshitiz|dba.se

sqlite> .mode column
sqlite> select * from foo;
234         kshitiz     dba.se

sqlite> .headers on
sqlite> select * from foo;
bar         baz         baf
----------  ----------  ----------
234         kshitiz     dba.se
like image 33
Talha Mughal Avatar answered Sep 24 '22 08:09

Talha Mughal