Skip to main content

How to Write Comments in SQL for SQLite

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Comments are notes in a SQL script that SQLite skips: what a table is for, or why a column exists. SQL has two kinds, -- for the rest of a line and /* */ for several lines. This guide comments a packing list, with real output from SQLite 3.54.1.

Syntax​

Syntax
-- <comment until the end of the line>

/* <comment that can
span several lines> */
PartWhat it means
--Starts a comment that runs to the end of the line. It can follow code on the same line
/* and */Everything between them is a comment, over as many lines as you like

SQLite's documentation on comments: "SQL comments begin with two consecutive "-" characters (ASCII 0x2d) and extend up to and including the next newline character (ASCII 0x0a) or until the end of input, whichever comes first."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as comments.sql:

comments.sql
-- Things to pack for a weekend trip.
CREATE TABLE packing (
item TEXT NOT NULL,
quantity INTEGER NOT NULL -- how many to bring
);

/* Two items for now.
More can be added later. */
INSERT INTO packing (item, quantity) VALUES ('Socks', 3);
INSERT INTO packing (item, quantity) VALUES ('Map', 1);

SELECT * FROM packing;

The comments.sql script in a macOS-style code window: a double-dash comment on the first line, a packing table whose quantity column ends with the comment how many to bring, a two-line block comment, two inserts, Socks 3 and Map 1, and a SELECT of every row.

  • The first line is a -- comment on a line of its own.
  • -- how many to bring follows code on the same line.
  • The /* ... */ comment spans two lines.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table packing.db ".read comments.sql"

sqlite3 opens the database file packing.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+-------+----------+
| item | quantity |
+-------+----------+
| Socks | 3 |
| Map | 1 |
+-------+----------+

The output in a terminal: the packing table with Socks at quantity 3 and Map at quantity 1.

SQLite skipped all three comments and ran the rest: the table, the two rows and the SELECT.

Common Mistakes​

MistakeWhat happensDo this instead
# after a statement, as in SELECT 1 AS one; # trailingThe SELECT ran, then unrecognized token: "#"Use --
A /* */ comment inside anothernear "outer": syntax error: the first */ ended the whole commentDon't nest them. The documentation: "Comments do not nest."

Troubleshooting​

SymptomLikely causeFix
unrecognized token: "#"# isn't a comment in SQLUse --
A syntax error just after a */One /* */ comment inside anotherClose each comment before starting the next
Parse error near line 2 of comments.sql: table packing already existsThe script already ran on packing.dbDelete packing.db and run the script again

FAQ​

Does the sqlite3 shell accept # comments?​

Only as a whole line that starts with #: the shell skips that line, and SQLite never sees it. The shell's documentation: "The CLI also accepts whole-line comments that begin with a '#' character and extend to the end of the line. There can be no whitespace prior to the initial '#'." In SQL itself, # is an error, as Common Mistakes shows.

Where can a comment go?​

Anywhere a space could. The documentation: "Comments can appear anywhere whitespace can occur, including inside expressions and in the middle of other SQL statements."

Conclusion​

-- and /* */ added notes to the packing script, and SQLite skipped them and created and filled the table as if they weren't there.

Additional Resources​