Q 1. What is Python - MySQL connectivity?
Python–MySQL connectivity allows a Python program to communicate with a MySQL database. For example, a school application can take a student’s roll number as input, query the database, and display the student’s marks.
Basic workflow
- Import the MySQL connector.
- Establish a connection to the database.
- Create a cursor.
- Execute an SQL statement.
- Fetch results for
SELECT, or commit changes for INSERT, UPDATE, and DELETE. - Close the cursor and the connection.
Q2. Which Python module is required to connect to a MySQL database?
The mysql.connector module is used to connect Python to a MySQL database. It is provided by the mysql-connector-python package.
import mysql.connector
OR
The pymysql module is used to connect Python to a MySQL database. It is provided by the pymsql package.
import pymysql
Q3. How do you check whether Python - MySQL Connector module is installed on your computer?
Open the Python IDLE Shell, type the following command, and press Enter:
import mysql.connector
OR
import pymsql
- If the
>>> prompt returns without an error, MySQL Connector/Python is installed and can be imported by the Python version used by IDLE.
- If you get
ModuleNotFoundError: No module named 'mysql' or No module named 'mysql.connector', the connector is unavailable in that Python environment.
Q4. How do you install MySQL Connector/Python?On Windows, open Command Prompt, change to the Python Scripts folder that contains pip.exe, and run the installation command.
To find the location of pip.exe, type:
where pip
Change to the folder shown in the output. For example:
cd C:\Users\spsharma\AppData\Local\Programs\Python\Python314\Scripts
Install MySQL Connector/Python:
pip install mysql-connector-python
OR
Install pymysql connector
pip install pymysql
Open the Python IDLE Shell and verify the installation:
import mysql.connector
import pymysql
Q5. How do you create a connection between Python and MySQL?
Import the mysql.connector module and call its connect() function with your MySQL server details.
Type 1:
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", password="root",
database="school")
print("Connected to MySQL successfully.")
con.close()
Type 2:
import mysql.connector as my
con = my.connect(host="localhost", user="root", password="root", database="school")
Type 3:
import mysql.connector as my
con = my.connect(host="127.0.0.1", user="root", password="root", database="school")
Type 4:
import mysql.connector as my
con = my.connect(host="127.0.0.1", username ="root", password="root", database="school")
Type 5:
import mysql.connector as my
con = my.connect(host="127.0.0.1", user="root", password="root")
Type 6:
import mysql.connector as my
con = my.connect(host="127.0.0.1", user="root", password="root", database="school")
connect() method Parameters Meaning
host
Use localhost or 127.0.0.1 when the MySQL server is running on the same computer.
OR Use IP address of the system if MySQL is running in another computer.
username or user
The MySQL username. Replace root if you use a different account.
In pymysql use user parameter not username
password
The password for that MySQL account. It can be root or any other
You can use password or passwd as parameter
database
The name of the existing database you want to use.
It is optional