UPDATE statement in SQLite modifies existing records in a table. Using SET, we can change a specific column while keeping other columns unchanged, and WHERE helps target particular rows.
Syntax
UPDATE table_name
SET column_name = value
WHERE condition;
Explanation:
- UPDATE specifies the table whose data needs modification.
- SET assigns a new value to the selected column.
- WHERE specifies which rows should be updated.
- Without WHERE, the specified column is updated in every row.
Updating Specific Column
Following program creates a STUDENTS table, inserts sample records, updates one student's name, and changes the address of students who satisfy a condition.
import sqlite3
connection = sqlite3.connect(":memory:")
try:
connection.execute("""
CREATE TABLE STUDENTS (
SID INTEGER PRIMARY KEY,
SNAME TEXT NOT NULL,
SAGE INTEGER NOT NULL,
ADDRESS TEXT
)
""")
connection.executemany(
"""
INSERT INTO STUDENTS (SID, SNAME, SAGE, ADDRESS)
VALUES (?, ?, ?, ?)
""",
[
(1, "Mohan", 22, "Ponnur"),
(2, "Sudheer", 28, "Chebrolu"),
(3, "Neha", 22, "Tenali"),
],
)
connection.commit()
print("Original Table:")
for row in connection.execute(
"SELECT * FROM STUDENTS ORDER BY SID"
):
print(row)
connection.execute(
"UPDATE STUDENTS SET SNAME = ? WHERE SID = ?",
("Sravan", 1),
)
connection.execute(
"UPDATE STUDENTS SET ADDRESS = ? WHERE SAGE = ?",
("Naga", 22),
)
connection.commit()
print("\nUpdated Table:")
for row in connection.execute(
"SELECT * FROM STUDENTS ORDER BY SID"
):
print(row)
finally:
connection.close()
Output

Explanation:
- sqlite3.connect(":memory:") creates an in-memory database for this example.
- CREATE TABLE defines four columns: SID, SNAME, SAGE, and ADDRESS.
- executemany() inserts three sample records using parameter placeholders.
- First UPDATE changes SNAME only for the row where SID equals 1.
- Second UPDATE changes ADDRESS for every row where SAGE equals 22.
- Question-mark placeholders pass values separately from the SQL statements, helping prevent SQL injection.
- commit() saves the changes, while SELECT retrieves the modified records.
- Finally block closes the database connection.