Update rows

EasySQLite Programs · 5 of 20

Write a Python program to add 5 bonus marks to students below 80.

Advertisement
Output
updated: 2
[('Asha', 91), ('Ravi', 79), ('John', 70)]

Approach

  1. UPDATE ... SET marks = marks + 5 WHERE ...; rowcount tells how many rows changed.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE students (name TEXT, marks INTEGER)")conn.executemany("INSERT INTO students VALUES (?, ?)", [("Asha", 91), ("Ravi", 74), ("John", 65)])cur = conn.execute("UPDATE students SET marks = marks + 5 WHERE marks < 80")print("updated:", cur.rowcount)print(conn.execute("SELECT * FROM students").fetchall())
Output
updated: 2
[('Asha', 91), ('Ravi', 79), ('John', 70)]

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Leave a Comment