If you’re using one of SQLite’s tabular output modes, you might find yourself battling with long lines of text that result in all subsequent columns being pushed out far to the right. This can cause you to have to keep scrolling sideways as you peruse the data.
Fortunately, there’s an easy fix.
You can use the --wrap
option to set the maximum width of each column. Any column that contains text longer than that will wrap to the next line.
You can also use --wordwrap on
or its shortcut -ww
to ensure words don’t get cut off halfway through.
Example
Suppose we set our output mode to table
:
.mode table
At this stage, we still haven’t specified any wrap options.
Therefore, when we select columns with long text, it won’t wrap:
SELECT * FROM Album LIMIT 2;
Result:
+---------+---------------------------------------+----------+ | AlbumId | Title | ArtistId | +---------+---------------------------------------+----------+ | 1 | For Those About To Rock We Salute You | 1 | | 2 | Balls to the Wall | 2 | +---------+---------------------------------------+----------+
Now let’s set the wrap option:
.mode --wrap 20
Now let’s run the query again:
SELECT * FROM Album LIMIT 2;
Result:
+---------+----------------------+----------+ | AlbumId | Title | ArtistId | +---------+----------------------+----------+ | 1 | For Those About To R | 1 | | | ock We Salute You | | +---------+----------------------+----------+ | 2 | Balls to the Wall | 2 | +---------+----------------------+----------+
We can see that the first line has wrapped in accordance with our --wrap
settings.
I didn’t specify word wrap in this example and so in this instance, the word “Rock” was broken up – part of it stayed on the first line and part of it wrapped to the next line.
We can overcome this with --wordwrap on
or its shortcut -ww
:
.mode -wrap 20 -ww
That’s the same as doing the following:
.mode -wrap 20 --wordwrap on
Now let’s run the query again:
SELECT * FROM Album LIMIT 2;
Result:
+---------+---------------------+----------+ | AlbumId | Title | ArtistId | +---------+---------------------+----------+ | 1 | For Those About To | 1 | | | Rock We Salute You | | +---------+---------------------+----------+ | 2 | Balls to the Wall | 2 | +---------+---------------------+----------+
This time the word “Rock” is wrapped to the next line rather than being cut off.
Another Option: qbox
Mode
Another option for wrapping lines of text is to use qbox
output mode:
.mode qbox
That is a shortcut for the following:
.mode box --wrap 60 --quote
So this wraps lines at 60 characters, and it quotes strings like an SQL literal.