Skip to main content

How to Insert a Row into a SQLite Table with INSERT INTO

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
INSERT INTO <table> (<column 1>, <column 2>) VALUES (<value 1>, <value 2>);
PartWhat 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 --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 insert_row.sql:

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 insert_row.sql script in a macOS-style code window: a books table with title, author and pages, an insert of Emma by Jane Austen with 474 pages, an insert of Persuasion by Jane Austen without pages, and a SELECT of every row.

  • The first INSERT names all three columns and gives three values, in the same order.
  • The second leaves pages out. The column allows NULL, so the row can go in without it.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
+------------+-------------+-------+
| title | author | pages |
+------------+-------------+-------+
| Emma | Jane Austen | 474 |
| Persuasion | Jane Austen | |
+------------+-------------+-------+

The output in a terminal: the books table with Emma by Jane Austen at 474 pages, and Persuasion by Jane Austen with an empty pages cell.

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​

MistakeWhat happensDo 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 earlyDouble it: 'Charlotte''s Web' stored Charlotte's Web
Fewer values than listed columns2 values for 3 columnsGive one value per listed column

Troubleshooting​

SymptomLikely causeFix
table books has 3 columns but 2 values were suppliedNo column list, and fewer values than the table has columnsList the columns you're giving values for
table books has no column named titelA misspelled column nameCheck the names with .schema books
Parse error near line 1 of insert_row.sql: table books already existsThe script already ran on library.dbDelete 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​