How to properly format sqlite shell output?

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

All of the answers provide settings you can type at the SQLite console or via CLI, but nobody mentions that these settings can be put into an RC file to avoid having to type them all the time. Save this as ~/.sqliterc:

.mode column
.headers on
.separator ROW "\n"
.nullvalue NULL

Note I've also added a placeholder for null values, instead of the default empty string.


For those that are interested in getting the same results, except running sqlite from command line. I found that the following doesn't work:

$ sqlite3 <dbfile> ".headers on;.mode column;select * from MyTable"
Error: mode should be one of: ascii column csv html insert line list tabs tcl

Instead, you have to use the options -column and -header with the sqlite command as follows:

$ sqlite3 -column -header <dbfile> "select * from MyTable"

Using:

$ sqlite3 --version 3.8.11.1 2015-07-29 20:00:57 cf538e2783e468bbc25e7cb2a9ee64d3e0e80b2f

Tags:

Sqlite