On this page
sqlite3.Cursor
Prev: sqlite3.Connection | Up: sqlite3 | Next: sqlite3.DatabaseError
Rel: sqlite3.adapt, sqlite3.Binary, sqlite3.Blob, sqlite3.complete_statement
Ref: docs.python.org — sqlite3.Cursor
Tags: #public #python3 #stdlib #sqlite3
Import
import sqlite3
from sqlite3 import Cursor
Signature & Overview
sqlite3.Cursor(...)
SQLite database cursor class.
Methods & Attributes
Cursor.close(self, /)— Closes the cursor.Cursor.execute(self, sql, parameters=(), /)— Executes an SQL statement.Cursor.executemany(self, sql, seq_of_parameters, /)— Repeatedly executes an SQL statement.Cursor.executescript(self, sql_script, /)— Executes multiple SQL statements at once.Cursor.fetchall(self, /)— Fetches all rows from the resultset.Cursor.fetchmany(self, /, size=1)— Fetches several rows from the resultset.Cursor.fetchone(self, /)— Fetches one row from the resultset.Cursor.setinputsizes(self, sizes, /)— Required by DB-API. Does nothing in sqlite3.Cursor.setoutputsize(self, size, column=None, /)— Required by DB-API. Does nothing in sqlite3.
Usage
import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE nums (n INTEGER)")
cur.executemany("INSERT INTO nums VALUES (?)", [(1,), (2,), (3,)])
assert [row[0] for row in cur.execute("SELECT n FROM nums ORDER BY n")] == [1, 2, 3]
con.close()
Gotchas
- Iterable directly: Prefer
for row in cur:orcur.fetchall()/cur.fetchone(). - Check
cur.lastrowidandcur.rowcount:rowcountis-1forSELECTstatements until rows are fetched.
REPL Inspect
from sqlite3 import Cursor
help(Cursor)
Linked from
Related notes
- tuts/python3/stdlib_module/sqlite3 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Connection Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/DatabaseError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/adapt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Binary Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Blob Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/complete_statement Linked here; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib