How to Write Comments in SQL for SQLite
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
-- <comment until the end of the line>
/* <comment that can
span several lines> */
| Part | What 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 --versionto 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:
-- 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 first line is a
--comment on a line of its own. -- how many to bringfollows code on the same line.- The
/* ... */comment spans two lines.
Step 2: Run It
In the folder that holds the script, run:
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.
+-------+----------+
| item | quantity |
+-------+----------+
| Socks | 3 |
| Map | 1 |
+-------+----------+

SQLite skipped all three comments and ran the rest: the table, the two rows and the SELECT.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
# after a statement, as in SELECT 1 AS one; # trailing | The SELECT ran, then unrecognized token: "#" | Use -- |
A /* */ comment inside another | near "outer": syntax error: the first */ ended the whole comment | Don't nest them. The documentation: "Comments do not nest." |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
unrecognized token: "#" | # isn't a comment in SQL | Use -- |
A syntax error just after a */ | One /* */ comment inside another | Close each comment before starting the next |
Parse error near line 2 of comments.sql: table packing already exists | The script already ran on packing.db | Delete 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
- SQL comment syntax — both kinds of comment.
- Command Line Shell For SQLite — how the shell reads scripts, including
#lines.
