0% found this document useful (0 votes)
0 views2 pages

PYTHON_MySQL_DB_Connectivity

The document outlines the steps for establishing database connectivity using the mysql.connector module in Python. It includes importing the module, creating a connection object, creating a cursor object, executing SQL queries, and cleaning up by closing the connection. Each step is accompanied by syntax examples for clarity.

Uploaded by

Usha Bharti P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

PYTHON_MySQL_DB_Connectivity

The document outlines the steps for establishing database connectivity using the mysql.connector module in Python. It includes importing the module, creating a connection object, creating a cursor object, executing SQL queries, and cleaning up by closing the connection. Each step is accompanied by syntax examples for clarity.

Uploaded by

Usha Bharti P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

(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()

You might also like