MySQL :: MySQL Connector/Python Developer Guide :: 10.6.1 cursor.MySQLCursorBuffered Class (original) (raw)
10.6.1 cursor.MySQLCursorBuffered Class
The MySQLCursorBuffered
class inherits fromMySQLCursor.
After executing a query, aMySQLCursorBuffered
cursor fetches the entire result set from the server and buffers the rows.
For queries executed using a buffered cursor, row-fetching methods such asfetchone() return rows from the set of buffered rows. For nonbuffered cursors, rows are not fetched from the server until a row-fetching method is called. In this case, you must be sure to fetch all rows of the result set before executing any other statements on the same connection, or anInternalError
(Unread result found) exception will be raised.
MySQLCursorBuffered
can be useful in situations where multiple queries, with small result sets, need to be combined or computed with each other.
To create a buffered cursor, use the buffered
argument when calling a connection'scursor() method. Alternatively, to make all cursors created from the connection buffered by default, use thebuffered
connection argument.
Example:
import mysql.connector
cnx = mysql.connector.connect()
# Only this particular cursor will buffer results
cursor = cnx.cursor(buffered=True)
# All cursors created from cnx2 will be buffered by default
cnx2 = mysql.connector.connect(buffered=True)
For a practical use case, seeSection 6.1, “Tutorial: Raise Employee's Salary Using a Buffered Cursor”.