Skip to main content

How to Insert Several Rows at Once in SQLite

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Adding rows one INSERT at a time works, but one INSERT can add many: list one set of values per row after VALUES. This guide adds three books in one statement, with real output from SQLite 3.54.1.

Syntax​

Syntax
INSERT INTO <table> (<column 1>, <column 2>) VALUES
(<value 1>, <value 2>),
(<value 1>, <value 2>);
PartWhat it means
(<value 1>, <value 2>)One list of values for each new row, in the order of the column list
, between the listsSeparates the rows. Only the last list is followed by ;

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_rows.sql:

insert_rows.sql
CREATE TABLE books (
title TEXT NOT NULL,
pages INTEGER NOT NULL
);

INSERT INTO books (title, pages) VALUES
('Emma', 474),
('Persuasion', 249),
('Dune', 412);

SELECT * FROM books;

The insert_rows.sql script in a macOS-style code window: a books table with title and pages, one INSERT whose VALUES holds three lists, Emma 474, Persuasion 249 and Dune 412, and a SELECT of every row.

  • One INSERT names the columns once, then gives three value lists, one per book.
  • A comma separates the lists, and the semicolon comes after the last one.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table library.db ".read insert_rows.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 | pages |
+------------+-------+
| Emma | 474 |
| Persuasion | 249 |
| Dune | 412 |
+------------+-------+

The output in a terminal: the books table with Emma at 474 pages, Persuasion at 249 and Dune at 412.

One statement added all three books, in the order of the lists.

Common Mistakes​

MistakeWhat happensDo this instead
A semicolon after each listThe first semicolon ends the statement, and the next line fails with near "(": syntax errorCommas between the lists, one semicolon at the end
A list with a value missing, such as ('Persuasion')all VALUES must have the same number of termsGive every list one value per column

Troubleshooting​

SymptomLikely causeFix
NOT NULL constraint failed: books.title, and no rows addedOne list broke a rule, so the whole statement was undoneFix that list and run the statement again
Parse error near line 1 of insert_rows.sql: table books already existsThe script already ran on library.dbDelete library.db and run the script again

FAQ​

What happens when one row breaks a rule?​

None of the rows go in. With a NULL title in the middle list, the statement failed with NOT NULL constraint failed: books.title, and the table stayed empty, Emma included. SQLite's documentation on conflicts: the default response "aborts the current SQL statement with an SQLITE_CONSTRAINT error and backs out any changes made by the current SQL statement".

Is one INSERT with many rows different from many INSERTs?​

The rows end up the same. The difference is what a failure undoes: one statement is all or nothing, while separate INSERT statements each succeed or fail on their own.

Conclusion​

One INSERT with three value lists added Emma, Persuasion and Dune together, and a bad row in the middle would have kept all three out.

Additional Resources​

  • INSERT — every form of the statement.
  • ON CONFLICT — what SQLite undoes when a row breaks a constraint.