The SQLite command line interface allows you to format your query results in TCL mode. Doing this encloses all output in double quotes, including the column headers if you’ve specified those. Any internal double quotes are escaped with a backslash.
Continue readingTag: how to
Output SQLite Query Results as a Tab-Separated List
The SQLite command line interface allows you to use various modes for formatting the result set of SQL queries. For example, you can format the results in columns, as a comma separated list, using vertical output, and more.
This article explains how to use the .mode
dot command to format your results as a tab-separated list.
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.
Output SQLite Query Results as an INSERT Statement
The SQLite command line interface has a handy dot command called .mode
, which allows you to change the way query results are formatted.
One of the values you can use with this command is insert
. Using this value results in all subsequent query results being formatted as an SQL INSERT
statement.
Format SQLite Query Results as an HTML Table
The SQLite command line shell has a .mode
dot command, which enables you to change the way SQLite formats its query results.
The default output for queries is as a pipe-separated list, however, you can use the .mode
dot command to change this to another format, such as an HTML table.
Format SQLite Query Results as a Comma-Separated List
SQLite has several modes and settings that enable you to output query results as a comma-separated list.
For example, you can use the .separator
dot command to specify a comma as the separator. Or you could use the “quote” mode to format the output as string literals.
However, for the purposes of this article, I’m going to use csv
mode, which formats the results as a comma-separated list, as well as with double quotes around strings. Internal double quotes are double-quoted.
Enclose Strings in Single Quotes in SQLite Query Results
When working with the SQLite command line shell, you have the option of having your query results formatted as string literals.
You can do this by switching to “quote” mode. When you do this, strings are enclosed in single-quotes and internal single-quotes are escaped by doubling. Also, blobs are displayed in hexadecimal blob literal notation, numbers are displayed as ASCII text, and NULL values are shown as “NULL”.
If you need to format your results with double quotes, consider using csv
mode.
Change the Separator to a Comma in SQLite Query Results
By default, the SQLite command line shell formats query output as a pipe-separated list, without column headers.
If you need to output the results using commas instead of the pipe-symbol, you can specify this with the .separator
dot command.
If you need to format your results as string literals, see How to Enclose Strings in Single Quotes. Or if you need any strings to be enclosed in double quotes, see How to Format Results as CSV.
Continue readingFormat SQLite Query Results as Columns with Column Headers
By default, when you connect to the SQLite command line shell and run a query, the result is returned as a pipe separated list.
You might have noticed that the results don’t include the column names, which can make your results confusing if you’re trying to read them, especially if the query returned many columns.
Fortunately, there’s an easy way to format the output so that it’s displayed as a column with column headers.
Continue reading5 Ways to Run an SQL Script from a File in SQLite
SQLite provides us with the ability to run scripts directly from a file. This can be especially useful when you have a large script (such as creating a bunch of database tables and inserting data into those tables).
Below are five ways to run SQL scripts directly from a file in SQLite.
Continue reading