GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python SQLite - Update Data - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python SQLite - Update Data

Last Updated : 27 Oct, 2026

UPDATE statement in SQLite modifies existing records in a database table. Python provides the sqlite3 module to execute UPDATE queries and change one or more columns according to specified conditions.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;

  • UPDATE specifies the table containing the records to modify.
  • SET assigns new values to the specified columns.
  • WHERE identifies the rows that should be updated.
  • Without WHERE, the statement updates every row in the table.

1. Setting Up SQLite Database

Before updating records, create a table and insert sample data. The following example uses an in-memory database so that the examples produce predictable results without modifying an existing database file.

Python
import sqlite3

conn = sqlite3.connect(":memory:")
cursor = conn.cursor()

cursor.execute("""
CREATE TABLE employees (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    department TEXT NOT NULL,
    salary INTEGER NOT NULL
)
""")

employees = [
    (1, "Aditi", "HR", 42000),
    (2, "Rahul", "IT", 65000),
    (3, "Neha", "IT", 72000),
    (4, "Rohan", "Sales", 48000)
]

cursor.executemany(
    """
    INSERT INTO employees (id, name, department, salary)
    VALUES (?, ?, ?, ?)
    """,
    employees
)

conn.commit()

Explanation:

  • sqlite3 provides the interface between Python and SQLite.
  • connect() establishes the database connection.
  • CREATE TABLE defines the employees table and its columns.
  • executemany() inserts the sample records using parameterized values.
  • commit() saves the transaction.

2. Updating Single Column

To change one column for a particular employee, use UPDATE with SET and a condition in WHERE.

Example: Change Aditi's salary to 45,000.

Python
cursor.execute(
    "UPDATE employees SET salary = ? WHERE id = ?",
    (45000, 1)
)

conn.commit()

print(cursor.execute(
    "SELECT * FROM employees WHERE id = ?",
    (1,)
).fetchone())

Output

(1, 'Aditi', 'HR', 45000)

Explanation:

  • SET modifies only the salary column.
  • WHERE id = ? selects the employee whose ID is 1.
  • Other column values remain unchanged.
  • fetchone() retrieves the updated record for verification.

3. Updating Multiple Rows Using Condition

UPDATE statement can modify several records that satisfy the same condition.

Example: Increase the salary of employees in the IT department by 2,000.

Python
cursor.execute(
    """
    UPDATE employees
    SET salary = salary + ?
    WHERE department = ?
    """,
    (2000, "IT")
)

conn.commit()

print(cursor.execute(
    """
    SELECT id, name, salary
    FROM employees
    WHERE department = ?
    ORDER BY id
    """,
    ("IT",)
).fetchall())

Output

[(2, 'Rahul', 67000), (3, 'Neha', 74000)]

Explanation:

  • WHERE department = ? selects employees belonging to IT.
  • SET salary = salary + ? adds 2,000 to each matching salary.
  • Both matching records are updated by one SQL statement.
  • fetchall() returns all matching records for verification.

4. Updating MUltiple Columns

Multiple columns can be modified in the same UPDATE statement by separating their assignments with commas.

Example: Change Rahul's department to Finance and set his salary to 68,000.

Python
cursor.execute(
    """
    UPDATE employees
    SET department = ?, salary = ?
    WHERE id = ?
    """,
    ("Finance", 68000, 2)
)

conn.commit()

print(cursor.execute(
    "SELECT * FROM employees WHERE id = ?",
    (2,)
).fetchone())

Output

(2, 'Rahul', 'Finance', 68000)

Explanation:

  • SET assigns new values to department and salary.
  • Commas separate the column assignments.
  • WHERE id = ? restricts the operation to Rahul's record.
  • Both changes are performed in a single query.

5. Updating All Rows

UPDATE statement does not contain a WHERE clause, SQLite applies the specified changes to every row in the target table. Use this approach only when modifying all records is intentional.

Example: Set the department of every employee to Operations.

Python
cursor.execute(
    "UPDATE employees SET department = ?",
    ("Operations",)
)

conn.commit()

Explanation:

  • SET assigns Operations to the department column.
  • Absence of WHERE means no rows are excluded.
  • Every employee's department is changed, not just one employee's.
  • Do not execute this query when only specific records should be modified.

6. Verifying Updated Data

After performing the updates, SELECT can retrieve the complete table to verify the changes.

Python
cursor.execute(
    """
    SELECT id, name, department, salary
    FROM employees
    ORDER BY id
    """
)

for row in cursor.fetchall():
    print(row)

Output

Screenshot_2026-10-26_12-22-04
Output

Explanation:

  • SELECT retrieves the specified columns.
  • ORDER BY id displays the records in ID order.
  • fetchall() returns the query results.
  • Output confirms that the single-column, multiple-row, and multiple-column updates were applied.
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.