(b) There are various steps for creating database connectivity as follows
Step 1
Import mysql.connector module
To import mysql.connector module, write the following command : import
mysql.connector Or import mysql.connector as mydb
Step 2
Create the connection object
To create a connection between the MySQL database and Python application, use
connect() method mysql.connector module is used. Pass the database details like
HostName, UserName and database Password. This method returns the connection
object.
Syntax
Connecti on_object
= mysql .connector.connect(
host = HostName,
user = UserName,
passwd = password
)
Step 3
Creating the cursor object
The cursor object is used to define as an abstraction that specified in Python DB-API
2.0. It provides the facility to the user to work on multiple separate environment through
the same connection to the database. Cursor object can be create using cursor()
method of connection object. It is an important term to the database for executing
queries.
Syntax
cursor_object = connection_object. cursorO
Step 4
Execute the Query
After creating the cursor, SQL query can be execute using execute() function.
Syntax
cursor_object.execute (query)
Step 5
Clean up the environment
This is the final step for creating database connectivity. There is necessary to close the
connection which you created.
Syntax
connection_object.close()