Search code examples
databasesqliteselectformatcommand

How to change the format of select output (SQLite)


On SQLite, I displayed the table "user" as shown below but the select output is not well-formatted so it's difficult to see:

sqlite> .header on
sqlite> select * from user;
id|first_name|last_name|age
1|Steve|Jobs|56
2|Bill|Gates|66
3|Mark|Zuckerberg|38

I want more well-formatted select output something like as shown below or any formats of select output are fine as long as the formats are better than above:

id|first_name|last_name |age
1 |Steve     |Jobs      |56
2 |Bill      |Gates     |66
3 |Mark      |Zuckerberg|38

Are there any ways to change the format of select output?


Solution

  • This command below sets the output mode "box":

    .mode box
    

    Then, this is how it looks like below:

    sqlite> .header on
    sqlite> select * from user;
    ┌────┬────────────┬────────────┬─────┐
    │ id │ first_name │ last_name  │ age │
    ├────┼────────────┼────────────┼─────┤
    │ 1  │ Steve      │ Jobs       │ 56  │
    │ 2  │ Bill       │ Gates      │ 66  │
    │ 3  │ Mark       │ Zuckerberg │ 38  │
    └────┴────────────┴────────────┴─────┘
    

    And, this command below sets the output mode "table":

    .mode table
    

    Then, this is how it looks like below:

    sqlite> .header on
    sqlite> select * from user;
    +----+------------+------------+-----+
    | id | first_name | last_name  | age |
    +----+------------+------------+-----+
    | 1  | Steve      | Jobs       | 56  |
    | 2  | Bill       | Gates      | 66  |
    | 3  | Mark       | Zuckerberg | 38  |
    +----+------------+------------+-----+
    

    There are 14 output modes in total as shown below:

    box         Tables using unicode box-drawing characters
    csv         Comma-separated values
    column      Output in columns.  (See .width)
    html        HTML <table> code
    insert      SQL insert statements for TABLE
    json        Results in a JSON array
    line        One value per line
    list        Values delimited by "|"
    markdown    Markdown table format
    qbox        Shorthand for "box --width 60 --quote"
    quote       Escape answers as for SQL
    table       ASCII-art table
    tabs        Tab-separated values
    tcl         TCL list elements
    

    And these commands show the details of the command ".mode":

    .help .mode
    

    Or:

    .help mode
    

    Then, this is how it looks like below:

    sqlite> .help .mode
    .import FILE TABLE       Import data from FILE into TABLE
       Options:
         --ascii               Use \037 and \036 as column and row separators
         --csv                 Use , and \n as column and row separators
         --skip N              Skip the first N rows of input
         --schema S            Target table to be S.TABLE
         -v                    "Verbose" - increase auxiliary output
       Notes:
         *  If TABLE does not exist, it is created.  The first row of input
            determines the column names.
         *  If neither --csv or --ascii are used, the input mode is derived
            from the ".mode" output mode
         *  If FILE begins with "|" then it is a command that generates the
            input text.
    .mode MODE ?OPTIONS?     Set output mode
       MODE is one of:
         ascii       Columns/rows delimited by 0x1F and 0x1E
         box         Tables using unicode box-drawing characters
         csv         Comma-separated values
         column      Output in columns.  (See .width)
         html        HTML <table> code
         insert      SQL insert statements for TABLE
         json        Results in a JSON array
         line        One value per line
         list        Values delimited by "|"
         markdown    Markdown table format
         qbox        Shorthand for "box --width 60 --quote"
         quote       Escape answers as for SQL
         table       ASCII-art table
         tabs        Tab-separated values
         tcl         TCL list elements
       OPTIONS: (for columnar modes or insert mode):
         --wrap N       Wrap output lines to no longer than N characters
         --wordwrap B   Wrap or not at word boundaries per B (on/off)
         --ww           Shorthand for "--wordwrap 1"
         --quote        Quote output text as SQL literals
         --noquote      Do not quote output text
         TABLE          The name of SQL table used for "insert" mode