How to connect MySQL database with Python with example?

To connect Python to a MySQL database, you can use the mysql-connector-python library. This library allows Python code to interact directly with a MySQL database.

How to install mysql-connector-python library:

Install mysql-connector-python using pip:

pip install mysql-connector-python

Run this command in your terminal editor or command prompt (cmd).

For example:

install mysql-connector-python


How to connect Python with MySQL Workbanch / XAMPP?

To connect Python with MySQL, here's a basic syntax structure to establish a connection:

import mysql.connector

# Connect to the database
connection = mysql.connector.connect(
    host="your_host",        # For local: 'localhost'
    user="your_username",     # Database username
    password="your_password", # Database password
    database="your_database"  # Name of the database to connect to
)

If you are using MySQL workbench, then host is always localhost, username is always root, password is "that you set while installing the mysql", or database "that you create in the MySQL".

Example:

This example connects to a MySQL database with Python:

import mysql.connector

connection = mysql.connector.connect(
                host="localhost",
                user="root",
                password="1234",
                database="saturday"
            )

if connection.is_connected():
    print("Successfully connected to the database")
else:
    print("Connection failed")

Output of this code is depends on: if all configrations are right.

Successfully connected to the database

if the database is not exist (error):

mysql.connector.errors.ProgrammingError: 1049 (42000): Unknown database 'satusrday'

if username or password is wrong:

mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

 

 


Five interview questions related to connecting Python with a MySQL database

Question 1. How do you connect to a MySQL database in Python, and what library do you use?

Answer: To connect to a MySQL database in Python, you can use the mysql-connector-python library. This library provides an interface for Python to interact with MySQL databases.

Example:

pip install mysql-connector-python

 

Question 2. What error would you see if the MySQL database specified in the connection does not exist? How can this be handled?

Answer: If the specified database does not exist, Python raises a mysql.connector.errors.ProgrammingError with error code 1049.

 

Question 3. How can you handle incorrect login credentials when connecting to MySQL using Python?

Answer: When incorrect login credentials are provided, MySQL raises a mysql.connector.errors.ProgrammingError with error code 1045. You can handle this with a try-except block.

Example:

try:
    connection = mysql.connector.connect(
        host="localhost",
        user="root",
        password="wrong_password",
        database="test_db"
    )
except mysql.connector.errors.ProgrammingError as e:
    print("Error:", e)

Output:

Error: 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

 

Leave a comment

You must be logged in to post a comment.

0 Comments