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 can I use an upsert statement to update data in a SQLite database?
- How can I use SQLite online?
- How do I use SQLite with Zephyr?
- How do I use regular expressions to query a SQLite database?
- How do I use the SQLite ZIP VFS to compress a database?
- How can I use SQLite to query for records between two specific dates?
- How can SQLite and ZFS be used together for software development?
- How can I use SQLite with Python to create a database?
- How do I use the SQLite zfill function?
- How do I extract the year from a datetime value in SQLite?
See more codes...