Cursors¶
PEP 249 cursors.
Cursor and DictCursor read the whole result set when
execute runs; SSCursor and SSDictCursor stream it,
reading rows from the server in batches as they are fetched.
PreparedCursor and PreparedDictCursor run server-side
prepared statements (the binary protocol).
Cursor
¶
Cursor(connection: Connection)
Buffered cursor: the whole result set is read when execute runs.
Rows are read in batches with the GIL released and each batch is
converted while the server sends the next one; the fetch* methods
then just hand them out. Ctrl+C interrupts a long read between batches.
arraysize
class-attribute
instance-attribute
¶
arraysize: int = 1
Default number of rows returned by fetchmany().
max_stmt_length
class-attribute
instance-attribute
¶
max_stmt_length: int = 1024000
Largest statement executemany() builds when batching inserts (PyMySQL's, under the 1 MiB max_allowed_packet of old servers).
max_pipeline
class-attribute
instance-attribute
¶
max_pipeline: int = 256
Statements executemany() sends before reading their replies, when it cannot batch them into one (anything but INSERT … VALUES). 1 sends each one after the previous reply.
rownumber
property
¶
rownumber: int | None
Index of the next row to fetch, or None without a result set.
execute
¶
Run query, interpolating args if given; return the rowcount.
Without args the query is sent as is, so % needs no escaping.
executemany
¶
Run query once per item of seq_of_args.
A single-row INSERT/REPLACE … VALUES (…) is sent as multi-row
statements of at most max_stmt_length bytes. Anything else
(UPDATE, DELETE…) runs once per item, pipelined: up to
max_pipeline statements (and 16 KiB) go to the server before
their replies are read, so a batch costs one round trip per window
instead of one per statement. The server runs every statement it
receives: if one fails, the rest of its window has already run when
the error is raised (no further window is sent). Inside a
transaction, rollback() undoes them all; with autocommit on, set
max_pipeline to 1 to stop right at the failing statement.
The cursor is left with the last statement's result. Returns the total number of affected rows.
callproc
¶
Call the stored procedure procname with args.
Arguments go through server variables named @_procname_n (n
counting from 0), so OUT and INOUT values can be read afterwards with
SELECT @_procname_0, …. The procedure's own result sets come
first, followed by the empty one of the CALL itself: read them
all with nextset(). Returns args unchanged.
procname may name the database too (db.proc): the first dot
separates them, so a procedure whose own name has a dot cannot be
called this way. The variables keep the whole name
(@_db.proc_0).
scroll
¶
Move to another row: value rows ahead (negative: back) in
relative mode, row number value in absolute mode.
Raises IndexError if that row does not exist.
DictCursor
¶
DictCursor(connection: Connection)
Bases: Cursor
Buffered cursor returning each row as a dict keyed by column name.
A column name that repeats an earlier one becomes table.column (the
table alias, empty for computed columns), as in other MySQL drivers.
SSCursor
¶
SSCursor(connection: Connection)
Bases: Cursor
Unbuffered cursor: rows stay on the server until they are fetched.
For result sets too large to hold in memory, or to start processing before the last row arrives. Rows are read in batches of about 4 KiB with the GIL released, so memory use does not grow with the result set.
Rules of the protocol, not of this driver:
- The connection cannot run another command until every row has been
read. One that does (from any cursor) first reads and discards the
remaining rows, with a
UserWarning; fetching from this cursor afterwards raisesProgrammingError.close()(or leaving awithblock) discards them silently. rowcountis -1 for result sets: the size is unknown until the end.scroll()only moves forward.- Rows the client is slow to read keep the server waiting; past its
net_write_timeout(60 s by default) it drops the connection.
rownumber
property
¶
rownumber: int | None
Index of the next row to fetch, or None without a result set.
execute
¶
Run query, interpolating args if given; return the rowcount
(-1 for a result set, whose size is unknown until the end).
fetchall
¶
fetchall() -> tuple[Row, ...]
Fetch every remaining row into a list (iterate the cursor instead to keep memory flat).
fetchall_unbuffered
¶
fetchall_unbuffered() -> Iterator[Row]
Iterate the remaining rows one by one, as iterating the cursor does (PyMySQL compatibility).
scroll
¶
Skip forward to another row (see Cursor.scroll()).
Skipped rows are read but never converted. Moving backwards raises
NotSupportedError; IndexError means the
result set ended first, leaving the cursor at its end.
SSDictCursor
¶
SSDictCursor(connection: Connection)
Bases: SSCursor
Unbuffered cursor returning each row as a dict (see SSCursor
and DictCursor).
PreparedCursor
¶
PreparedCursor(connection: Connection)
Bases: Cursor
Buffered cursor running server-side prepared statements.
The query's %s / %(name)s placeholders become ? (%% a
literal %) and it is prepared once per connection: the
statement_cache_size most recent statements stay prepared. Parameters
travel as typed values (the binary protocol), never as SQL text, so
nothing depends on escaping. Rows come back as the same objects a
Cursor returns.
A query without placeholders has nothing to bind and runs as with a
Cursor, through the text protocol: DDL and introspection
statements behave as libraries expect (the server cannot prepare some of
them, and answers others differently when prepared). One with
placeholders the server cannot prepare raises its error (1295).
Parameters are typed values only: a sequence (IN %s) or a type with
a conv= encoder raises NotSupportedError.
max_stmt_length
class-attribute
instance-attribute
¶
max_stmt_length: int = 256 * 1024
Largest statement executemany() builds when batching inserts: smaller than a Cursor's, since the server prepares each statement and larger ones measured slower.
executemany
¶
Run the prepared query once per item of seq_of_args.
As with Cursor.executemany(), a single-row INSERT/
REPLACE … VALUES (…) becomes multi-row statements (prepared, with
the same number of rows each so that the statement is reused; at
most max_stmt_length bytes of parameters), and other
statements are pipelined: up to max_pipeline
executions go to the server before their replies are read, and an
error stops the batch at the end of its window. Returns the total
number of affected rows; the cursor keeps the last execution's
result.
PreparedDictCursor
¶
PreparedDictCursor(connection: Connection)
Bases: PreparedCursor
PreparedCursor returning each row as a dict, as
DictCursor does.