Python MySQL - Cursor Object

In the following tutorial, we will discuss about the Cursor Object of the Python's MySQL library.

Understanding the MySQL - Cursor Object in Python

The mysql-connector-python (and related libraries) MySQLCursor is used to run commands in order to interact with the MySQL database. You may run procedures, execute SQL statements, and get data from result sets using its methods.

The cursor() function of the Connection object/class can be used to construct a Cursor object.

Example:

Methods:

The several methods that the Cursor class/object offers are listed below.

Sr. NoMethod & Description
1.callproc()
This method draws upon pre-existing MySQL database methods.
2.close ()
To close the current cursor object, use this function.
3.Info()
This methods provides details on the previous query.
4.executemany()
This procedure accepts a list of parameters in series. It creates and runs a MySQL query with all of its arguments included.
5.execute()
This function runs a MySQL query that is sent in as an argument.
6.fetchall()
Using this method, all the rows in a query's result set are retrieved and returned as a list of tuples. (If we run this after obtaining a few rows, the remaining rows are returned.)
7.fetchone()
This function retrieves the subsequent row from a query's result and returns it as a tuple.
8.fetchmany()
This function is comparable to fetchone(), except instead of retrieving a single row, it obtains the subsequent set of rows from the query's result set.
9.etchwarnings()
The warnings produced by the most recent query execution are returned by this method.

Properties

The following are the properties of the Cursor class:

Sr. NoProperty & Description
1.column_names
This read-only property produces a list with the names of a result set's columns.
2.description
This read-only property returns the list that contains the descriptions of the columns in a result-set.
3.lastrowid
This field is read-only and returns the value created for any auto-incremented columns in the table from the most recent INSERT or UPDATE action.
4.rowcount
This gives back how many rows were modified or returned during select and update operations.
5.statement
This property yields the statement that was last performed.