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 to configure SQLite with XAMPP on Windows?
- How can I use an upsert statement to update data in a SQLite database?
- How do I use SQLite with Visual Studio?
- How do I install SQLite on Windows?
- How do I use SQLite UNION to combine multiple SELECT queries?
- How do I use variables in a SQLite database?
- How do I show the databases in SQLite?
- How do I use regular expressions to query a SQLite database?
- How do I install and use SQLite on Ubuntu?
- How do I set up an ODBC driver to connect to an SQLite database?
See more codes...