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:
- Imports the
sqlite3
module. - Creates a connection to the database.
- Creates a cursor object.
- Executes a query to select all rows from the
employees
table. - Calls the
fetchall()
method to retrieve all rows of the query result. - Iterates over the rows and prints them.
Helpful links
More of Sqlite
- How do I use the SQLite ZIP VFS to compress a database?
- How to configure SQLite with XAMPP on Windows?
- How do I use an array in SQLite?
- How can I use SQLite with Zabbix?
- How do I exit SQLite?
- How do I extract the year from a datetime value in SQLite?
- How can I use SQLite to query for records between two specific dates?
- How do I use the SQLite VARCHAR data type?
- How do I call sqlitepcl.raw.setprovider() when using SQLite?
- How do I generate XML output from a SQLite database?
See more codes...