SQLite INSERT INTO Statement
The INSERT INTO Statement
The INSERT INTO statement adds new records to a table.
You can write the statement in two ways. The first way names the columns, and the values for them:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
If you give a value for every column, in the same order as in the table, you can leave out the column names:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
INSERT INTO Example
This example adds a new customer to the Customers table:
Example
INSERT INTO Customers (
CustomerName, ContactName, Address,
City, PostalCode, Country
)
VALUES (
'Cardinal', 'Emil Nilsson', 'Skagen 21',
'Stavanger', '4006', 'Norway'
);
Try it Yourself »
We did not give a value for the CustomerID column. It is an INTEGER PRIMARY KEY column, so SQLite gives the new record the next number automatically.
Tip: To see the new customer, run SELECT * FROM Customers ORDER BY CustomerID DESC; in the editor.
Insert Data Only in Some Columns
You can insert data in some of the columns only. The other columns get the value NULL:
Example
INSERT INTO Customers
(CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
Try it Yourself »
Insert More Than One Record
To insert many records with one statement, separate the records with commas:
Example
INSERT INTO Customers
(CustomerName, City, Country)
VALUES
('Cardinal', 'Stavanger', 'Norway'),
('Greasy Burger', 'Oslo', 'Norway'),
('Tasty Tee', 'Bergen', 'Norway');
Try it Yourself »
What Does INSERT Return?
An INSERT INTO statement returns no rows. It only adds the data.
When you run an INSERT INTO in our editor, you see the message "You have made changes to the database. Rows affected: 1". That message is written by the editor, not by SQLite. It tells you how many rows were inserted.
Count the Inserted Rows
SQLite remembers how many rows the last INSERT, UPDATE or DELETE changed. The changes() function returns that number. This example inserts three customers, and then shows how many rows were inserted:
Example
INSERT INTO Customers
(CustomerName, City, Country)
VALUES
('Cardinal', 'Stavanger', 'Norway'),
('Greasy Burger', 'Oslo', 'Norway'),
('Tasty Tee', 'Bergen', 'Norway');
SELECT changes();
Try it Yourself »
See the Inserted Rows
Add RETURNING at the end, and the INSERT INTO returns the new rows, like a SELECT. It is a good way to see the CustomerID that SQLite gave the new customer:
Example
INSERT INTO Customers
(CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway')
RETURNING CustomerID, CustomerName;
Try it Yourself »
Tip: Read more in the SQLite RETURNING chapter. The last_insert_rowid() function also returns the id of the last inserted row. In Python, use cur.lastrowid. See the Python SQLite Insert chapter.
When the Record Already Exists
SQLite has special ways to handle a new record that would break a rule, for example a primary key that is already used. Read more in the SQLite UPSERT chapter.