0% found this document useful (0 votes)
14 views

Cursors in Python

The MySQLCursor is used to execute SQL statements and call procedures on the MySQL database. It provides methods like execute(), fetchall(), fetchone() to run queries and retrieve result sets. The Cursor object is created using the cursor() method of the Connection object after establishing a connection.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Cursors in Python

The MySQLCursor is used to execute SQL statements and call procedures on the MySQL database. It provides methods like execute(), fetchall(), fetchone() to run queries and retrieve result sets. The Cursor object is created using the cursor() method of the Connection object after establishing a connection.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The MySQLCursor of mysql-connector-python (and similar libraries) is used to

execute statements to communicate with the MySQL database.


Using the methods of it you can execute SQL statements, fetch data from the result
sets, call procedures.
You can create Cursor object using the cursor() method of the Connection
object/class.
Example
import mysql.connector

#establishing the connection


conn = mysql.connector.connect(
user='root', password='password', host='127.0.0.1',
database='mydb'
)
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
Methods
Following are the various methods provided by the Cursor class/object.

Sr.No Method & Description

1
callproc()
This method is used to call existing
procedures MySQL database.

2
close()
This method is used to close the
current cursor object.

3
Info()
This method gives information about
the last query.

4
executemany()
This method accepts a list series of
parameters list. Prepares an MySQL
query and executes it with all the
parameters.
5
execute()
This method accepts a MySQL query
as a parameter and executes the given
query.

6
fetchall()
This method retrieves all the rows in
the result set of a query and returns
them as list of tuples. (If we execute
this after retrieving few rows it returns
the remaining ones)

7
fetchone()
This method fetches the next row in the
result of a query and returns it as a
tuple.

8
fetchmany()
This method is similar to the fetchone()
but, it retrieves the next set of rows in
the result set of a query, instead of a
single row.

9
fetchwarnings()
This method returns the warnings
generated by the last executed query.

You might also like