The Cursor of the
mysql-connector Python module is used to execute statements to communicate with the MySQL database.
We can create a Cursor object using the
cursor() method of the connection object.
import mysql.connector # Establishing the connection con = mysql.connector.connect( user='root', password='password', host='localhost', database='mydb' ) # creating a cursor object # using the cursor() method cursor = con.cursor()
Given below are the various methods provided by the Cursor object.
- callproc() – this method is used to call existing procedures MySQL database.
- close() – this method is used to close the current cursor object.
- Info() – this method gives information about the last query.
- execute() – this method accepts a MySQL query as a parameter and executes the given query.
- executemany() – this method accepts a list series of parameters list and prepares an MySQL query and executes it with all the parameters.
- fetchone() – this method fetches the next row in the result of a query and returns the result as a tuple.
- fetchall() – this method retrieves all the rows in the result set of a query and returns them as a list of tuples.
- fetchmany() – this method is similar to the
fetchone()method. But, it retrieves the next set of rows in the result set of a query, instead of a single row.
- etchwarnings() – returns the warnings generated by the last executed query.