9951 explained code solutions for 126 technologies


sqliteHow do I use the sqlite fetchall method?


The sqlite fetchall method is used to retrieve all rows of a query result in a single call. It returns a list of tuples containing all the rows of the query result. The syntax of the fetchall method is as follows:

cursor.fetchall()

The following example shows how to use the fetchall method to retrieve all the rows of a query result:

import sqlite3

conn = sqlite3.connect("mydatabase.db")
cursor = conn.cursor()

cursor.execute("SELECT * FROM employees")
rows = cursor.fetchall()

for row in rows:
    print(row)

# Output:
# (1, 'John', 'Doe', '[email protected]')
# (2, 'Jane', 'Doe', '[email protected]')

The code above:

  1. Imports the sqlite3 module.
  2. Creates a connection to the database.
  3. Creates a cursor object.
  4. Executes a query to select all rows from the employees table.
  5. Calls the fetchall() method to retrieve all rows of the query result.
  6. Iterates over the rows and prints them.

Helpful links

Edit this code on GitHub