In an sqlite3 connection, cursor objects acts as an intermediary between the connected SQLite database and Python code. Cursors are especially useful when executing queries that may yield data from the database such as those involving the SELECT sql command.
Consider the following example:
Table created successfully.
Rows inserted successfully.
In the above example, we created an SQLite database called points.db. We then created a table in the database called point with columns x
and y
, to represent the x and y coordinates of a point object. And finally we inserted three rows into the point
table.
Consider what we would do if we wanted to retrieve rows from the database. The conn
object created by sqlite3.connect()
function contain the methods for executing sql queries such as execute()
, executemany
and executescript
. However, these methods do not store the results of the query if any. In order to store and access the results, we need to execute the query through a cursor object.
conn.cursor()
objects
Cursor objects contains the three query executing methods i.e execute()
, executemany(
) and executescript()
. The three methods have the following syntax and usage.
execute() |
Executes a single sql statement. |
executemany() |
Repeatedly executes an sql statement. |
executescript() |
Executes multiple sql statements at once. |
In addition to the querying methods shown above, cursor
objects also contains methods for retrieving the results of an executed query, the fetching methods are fetchone()
, fetchmany()
and fetchall()
.
Consider the following example:
(3.0, 4.0)
In the above example, we used the fetchone()
method to retrieve a single row from the results yielded by the SELECT
query. The fetchone()
method returns one row from the query results, it remembers where it left off. This means that subsequent fetchone()
call will retrieve the next row in the result, as shown in the following example:
(3.0, 4.0)
(2.0, 5.0)
(4.0, 6.0)
Calling the cursor.close()
method is vital for ensuring that the cursor is closed and its resources are released.
Fetch multiple rows
The cursor.fetchmany()
method takes an integer argument that specifies the number of rows to be retrieved from the query result. It has the following syntax:
cursor.fetchmany(n = 1)
n
is the number of rows to be retrieved from the result, it defaults to 1
.
It returns a list of n
tuples where each tuple represents a fetched row.
[(3.0, 4.0), (2.0, 5.0)]
Fetch all rows
To fetch all returned rows, use the cursor.fetchall()
method.
[(3.0, 4.0), (2.0, 5.0), (4.0, 6.0)]
Cursor objects are also iterable meaning that we can for example use them directly in for loops to iterate through the returned rows without directly calling any of the three fetch methods.
import sqlite3
#connect to the database
with sqlite3.connect('points.db') as conn:
#create a cursor
cursor = conn.cursor()
cursor.execute('SELECT x, y FROM point;')
for row in cursor:
print(row)
cursor.close()
(3.0, 4.0)
(2.0, 5.0)
(4.0, 6.0)
Summary
The cursor
methods are summarized in the following table:
execute() |
Executes a single sql statement. |
executemany() |
Repeatedly executes an sql statements. |
executescript() |
Executes multiple sql statements at once. |
fetchone() |
Returns one row from the result. |
fetchmany(n) |
Returns n rows from the result. |
fetchall() |
Returns all rows from the result. |
close() |
Closes the cursor |