Documentation Home
MySQL Connector/Python Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb


10.2.6 MySQLConnection.cursor() Method

Syntax:

cursor = cnx.cursor([arg=value[, arg=value]...])

This method returns aMySQLCursor() object, or a subclass of it depending on the passed arguments. The returned object is acursor.CursorBase instance. For more information about cursor objects, seeSection 10.5, “cursor.MySQLCursor Class”, andSection 10.6, “Subclasses cursor.MySQLCursor”.

Arguments may be passed to thecursor() method to control what type of cursor to create:

  • Ifbuffered isTrue, the cursor fetches all rows from the server after an operation is executed. This is useful when queries return small result sets.buffered can be used alone, or in combination with thedictionary argument.

    buffered can also be passed toconnect() to set the default buffering mode for all cursors created from the connection object. SeeSection 7.1, “Connector/Python Connection Arguments”.

    For information about the implications of buffering, seeSection 10.6.1, “cursor.MySQLCursorBuffered Class”.

  • Ifraw isTrue, the cursor skips the conversion from MySQL data types to Python types when fetching rows. A raw cursor is usually used to get better performance or when you want to do the conversion yourself.

    raw can also be passed toconnect() to set the default raw mode for all cursors created from the connection object. SeeSection 7.1, “Connector/Python Connection Arguments”.

  • Ifdictionary isTrue, the cursor returns rows as dictionaries. This argument is available as of Connector/Python 2.0.0.

  • Ifprepared isTrue, the cursor is used for executing prepared statements. This argument is available as of Connector/Python 1.1.2. The C extension supports this as of Connector/Python 8.0.17.

  • Thecursor_class argument can be used to pass a class to use for instantiating a new cursor. It must be a subclass ofcursor.CursorBase.

The returned object depends on the combination of the arguments. Examples:

  • If not buffered and not raw:MySQLCursor

  • If buffered and not raw:MySQLCursorBuffered

  • If not buffered and raw:MySQLCursorRaw

  • If buffered and raw:MySQLCursorBufferedRaw