Display SQLite Query Results using Vertical Output

One of the values you can provide to the .mode dot command when using the SQLite command line shell is line.

Using .mode line outputs the query results using vertical output. What I mean is that, each column is displayed on a new line. The column name is displayed, along with an equals sign and then the column’s value.

Example

Here’s an example to demonstrate.

.mode line
SELECT * FROM Products;

Result:

  ProductId = 1
ProductName = Widget Holder
      Price = 139.5


  ProductId = 2
ProductName = Widget Opener
      Price = 89.7


  ProductId = 3
ProductName = Widgets - 6 Pack
      Price = 374.2


  ProductId = 4
ProductName = Blue Widget
      Price = 63.0

So just to be clear, ProductId, ProductName, and Price are the column names in this example.

Save this Setting

The default setting for SQLite is .mode list, using a pipe-separated list. Therefore, when you connect to SQLite from a new terminal window, it will reset to this default setting.

However, you can save this setting in a configuration file so that you don’t have to re-enter the above code every time you connect to SQLite from a new terminal window.

To do this, enter the following into a text file:

.mode line

Then save the file as .sqliterc in your home directory.

Each time you connect to SQLite using the command line shell, your queries will be formatted using vertical output like in the above example.