Connecting Python to a MySQL database is a fundamental skill for backend developers, data analysts, and software engineers. Python is widely used for web applications, automation, and data processing, while MySQL is one of the most popular relational database management systems. When combined, Python and MySQL allow developers to build powerful, data-driven applications.
This guide explains how to connect Python to a MySQL database step by step. It is designed for beginners and intermediate learners and includes real-world use cases, practical examples, and best practices.
Python MySQL integration is commonly used in real-world applications where structured data storage and retrieval are required.
Before connecting Python to MySQL, ensure the following requirements are met.
| Requirement | Description |
|---|---|
| Python | Python 3.x installed on your system |
| MySQL Server | MySQL database installed and running |
| MySQL Client Library | mysql-connector-python or PyMySQL |
This is the official MySQL driver provided by Oracle. It is reliable, secure, and widely used for production applications.
PyMySQL is a pure Python MySQL client library. It is lightweight and easy to use, making it suitable for smaller projects.
To install the official MySQL connector, use the following command.
pip install mysql-connector-python
Before connecting Python to MySQL, create a database and a table.
CREATE DATABASE company_db; USE company_db; CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100), salary DECIMAL(10,2) );
The following example demonstrates how to establish a connection between Python and MySQL.
import mysql.connector connection = mysql.connector.connect( host="localhost", user="root", password="your_password", database="company_db" ) if connection.is_connected(): print("Connected to MySQL database")
cursor = connection.cursor() query = "INSERT INTO employees (name, email, salary) VALUES (%s, %s, %s)" values = ("Anita Sharma", "anita@example.com", 50000) cursor.execute(query, values) connection.commit() print("Record inserted successfully")
cursor.execute("SELECT * FROM employees") records = cursor.fetchall() for row in records: print(row)
update_query = "UPDATE employees SET salary = %s WHERE id = %s" cursor.execute(update_query, (55000, 1)) connection.commit()
delete_query = "DELETE FROM employees WHERE id = %s" cursor.execute(delete_query, (1,)) connection.commit()
In a real-world employee management system, Python connects to MySQL to:
This integration ensures secure data handling and fast performance for enterprise applications.
try: connection = mysql.connector.connect( host="localhost", user="root", password="your_password", database="company_db" ) except mysql.connector.Error as error: print("Error:", error) finally: if connection.is_connected(): cursor.close() connection.close() print("MySQL connection closed")
Connecting Python to a MySQL database is a critical skill for modern application development. By using libraries like mysql-connector-python, developers can easily perform database operations efficiently and securely. This guide covered installation, connection setup, CRUD operations, real-world examples, and best practices to help you confidently integrate Python with MySQL.
mysql-connector-python is the official and most reliable library for connecting Python to MySQL, especially for production use.
Yes, Python can connect to remote MySQL databases by specifying the remote host IP and ensuring firewall and MySQL permissions are configured.
No, MySQL can be hosted on a remote server or cloud platform while Python connects to it over the network.
Always use parameterized queries instead of string concatenation when executing SQL statements.
Yes, Python frameworks like Django and Flask use MySQL extensively for backend database operations.
Copyrights © 2024 letsupdateskills All rights reserved