SQLite UPDATE Statement
The UPDATE Statement
The UPDATE statement changes existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Warning: Be careful when you update records. The WHERE clause decides which records are changed. If you leave out WHERE, all the records in the table are changed!
Update a Record
This example gives the first customer (CustomerID = 1) a new contact person and a new city:
Example
UPDATE Customers
SET ContactName = 'Lene Nilsson', City = 'Frankfurt'
WHERE CustomerID = 1;
Try it Yourself »
Update Many Records
The WHERE clause decides how many records are updated. This example changes the postal code of all the customers from Mexico:
Update Warning!
If you leave out the WHERE clause, all the records are updated:
In the editor, you can click "Restore Database" to get the original data back. In a real database, you cannot!
Update With a Calculation
The new value can be calculated from the old value. This example raises the price of all the products in category 1 by 10%:
What Does UPDATE Return?
An UPDATE statement returns no rows. It only changes the data.
When you run an UPDATE 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 changed.
Count the Changed Rows
SQLite remembers how many rows the last INSERT, UPDATE or DELETE changed. The changes() function returns that number. This example changes the postal code of the customers from Mexico, and then shows how many rows were changed:
Example
UPDATE Customers
SET PostalCode = '00000'
WHERE Country = 'Mexico';
SELECT changes();
Try it Yourself »
See the Changed Rows
Add RETURNING at the end, and the UPDATE returns the changed rows, like a SELECT:
Example
UPDATE Customers
SET PostalCode = '00000'
WHERE Country = 'Mexico'
RETURNING CustomerName, PostalCode;
Try it Yourself »
Tip: Read more in the SQLite RETURNING chapter. In Python, cur.rowcount tells how many rows were changed. See the Python SQLite Update chapter.