Python Practice › SQLite Programs › Program 7
fetchone, fetchmany, fetchall
Write a Python program that shows the three fetch methods.
Output
(1,) [(2,), (3,)] [(4,), (5,), (6,)]
Approach
- Each call continues where the previous one stopped.
Solution
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE n (v INTEGER)")conn.executemany("INSERT INTO n VALUES (?)", [(i,) for i in range(1, 7)])cur = conn.execute("SELECT v FROM n")print(cur.fetchone(), cur.fetchmany(2), cur.fetchall())Output
(1,) [(2,), (3,)] [(4,), (5,), (6,)]
Learn the concept
New to this topic? Read the lesson, then come back.