94

Is there any way to configure sqlite3 so that the headers will display by default?

I know I can use .headers on to turn on headers, but I have to keep typing it every time I launch the client because the setting doesn't stick between sessions. I want the headers to be on permanently.

JFMR
  • 23,265
  • 4
  • 52
  • 76
spemmo
  • 943
  • 1
  • 6
  • 4
  • For anyone using Windows, [this answer](https://stackoverflow.com/questions/19147547/change-sqlite-default-settings-in-windows) provided a bit more clarity. – trobblob Aug 07 '22 at 00:55

2 Answers2

169

From the fine manual:

INIT FILE
sqlite3 reads an initialization file to set the configuration of the interactive environment. [...] If the file ~/.sqliterc exists, it is processed first. can be found in the user's home directory, it is read and processed. It should generally only contain meta-commands.

So just put a file called .sqliterc in your home directory and put this in it:

.headers ON
Demitri
  • 13,134
  • 4
  • 40
  • 41
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 5
    Be careful with the content of the ~/sqliterc file, comments are not allowed (lines begining with #), could stop interpretation of the config file. – Robert Lujo Nov 30 '17 at 11:11
  • 26
    I found adding `.mode columns` as well to .sqliterc made the results much easier to read. This will align the headers with the values. – Luke Oct 15 '19 at 23:53
  • 2
    Full list of "dot commands" can be found here: https://sqlite.org/cli.html#special_commands_to_sqlite3_dot_commands_. – Demitri Apr 26 '20 at 23:20
  • 3
    I've been using SQLite for over a decade, never knew this existed. Thank you! – Bill Apr 06 '21 at 21:54
34

You can pass arguments in the command line too:

sqlite3 db.db  -header -column  "select x from y;"
MaikoID
  • 4,359
  • 4
  • 25
  • 27