Delete rows

EasySQLite Programs · 6 of 20

Write a Python program to delete students with marks below 70.

Advertisement
Output
deleted: 1 [('Asha',), ('Ravi',)]

Approach

  1. DELETE FROM ... WHERE ....

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("DELETE FROM students WHERE marks < 70")print("deleted:", cur.rowcount, conn.execute("SELECT name FROM students").fetchall())
Output
deleted: 1 [('Asha',), ('Ravi',)]

Learn the concept

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

More sqlite programs

← All 20 SQLite Programs

Leave a Comment