How to Insert a Row into a SQLite Table with INSERT INTO
INSERT INTO adds a row to a table: you name the columns, then give a value for each. This guide adds two books, one with every value and one without its page count, with real output from SQLite 3.54.1.
Syntax
INSERT INTO <table> (<column 1>, <column 2>) VALUES (<value 1>, <value 2>);
| Part | What it means |
|---|---|
INSERT INTO <table> | The table that gets the new row |
(<column 1>, <column 2>) | The columns you're giving values for, in any order you like |
VALUES (<value 1>, <value 2>) | One value per listed column, in the same order. Text goes in single quotes |
A column you leave out gets its default value, or NULL when it has none. SQLite's documentation on INSERT: "The first form (with the "VALUES" keyword) creates one or more new rows in an existing table."
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 insert_row.sql:
CREATE TABLE books (
title TEXT NOT NULL,
author TEXT NOT NULL,
pages INTEGER
);
INSERT INTO books (title, author, pages) VALUES ('Emma', 'Jane Austen', 474);
INSERT INTO books (title, author) VALUES ('Persuasion', 'Jane Austen');
SELECT * FROM books;

- The first
INSERTnames all three columns and gives three values, in the same order. - The second leaves
pagesout. The column allowsNULL, so the row can go in without it.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table library.db ".read insert_row.sql"
sqlite3 opens the database file library.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.
+------------+-------------+-------+
| title | author | pages |
+------------+-------------+-------+
| Emma | Jane Austen | 474 |
| Persuasion | Jane Austen | |
+------------+-------------+-------+

Both rows went in. Persuasion's pages is empty: the INSERT left the column out, it has no default, so it holds NULL, which the table shows as an empty cell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Text in double quotes, "Emma" | no such column: "Emma" - should this be a string literal in single-quotes? | Use single quotes: 'Emma' |
An apostrophe inside text, 'Charlotte's Web' | near "s": syntax error: the apostrophe ends the text early | Double it: 'Charlotte''s Web' stored Charlotte's Web |
| Fewer values than listed columns | 2 values for 3 columns | Give one value per listed column |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
table books has 3 columns but 2 values were supplied | No column list, and fewer values than the table has columns | List the columns you're giving values for |
table books has no column named titel | A misspelled column name | Check the names with .schema books |
Parse error near line 1 of insert_row.sql: table books already exists | The script already ran on library.db | Delete library.db and run the script again |
FAQ
Do I have to list the columns?
No, but then you must give a value for every column, in the table's order. Two values for the three columns without a list gave table books has 3 columns but 2 values were supplied. With a list, it's also clear which value goes where.
How do I write text that contains a single quote?
Put two single quotes in a row. The documentation: "A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal."
Conclusion
INSERT INTO books (title, author, pages) VALUES ('Emma', 'Jane Austen', 474) added a full row, and leaving pages out added Persuasion without a page count.
Additional Resources
- INSERT — every form of the statement.
- Literal values — how to write text, numbers and
NULL.
