fetchone, fetchmany, fetchall

EasySQLite Programs · 7 of 20

Write a Python program that shows the three fetch methods.

Advertisement
Output
(1,) [(2,), (3,)] [(4,), (5,), (6,)]

Approach

  1. Each call continues where the previous one stopped.

Solution

Python
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.

More sqlite programs

← All 20 SQLite Programs

Leave a Comment