Python can store and read data in a MySQL database using the mysql-connector-python driver. This page is the overview of our Python MySQL series: it shows the complete flow in one place and links to a detailed tutorial for every step.
What You Will Learn
- Connect Python to MySQL
- Create a database and table
- Insert, select, update and delete safely
- Use LIMIT, ORDER BY and JOINs
- Avoid SQL injection
Prerequisites
MySQL Tutorial and SQL Basics.
Install and Connect
pip install mysql-connector-python
import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="your_password")
cur = mydb.cursor()
cur.execute("CREATE DATABASE IF NOT EXISTS itronix")
cur.execute("SHOW DATABASES")
for db in cur:
print(db)
('information_schema',)
('itronix',)
('mysql',)
('performance_schema',)
Explanation: XAMPP/WAMP users can check the new database in phpMyAdmin. In real apps never use root with an empty password – create a separate user.

Create a Table and Insert Rows
mydb = mysql.connector.connect(host="localhost", user="root", password="your_password", database="itronix")
cur = mydb.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255), address VARCHAR(255))""")
rows = [("Karan", "Ludhiana"), ("Kevin", "Dublin 4"), ("Keisha", "Otawa 4"),
("Varun", "Ontario"), ("Priya", "Delhi"), ("Atharv", "Goa")]
cur.executemany("INSERT INTO customers (name, address) VALUES (%s, %s)", rows)
mydb.commit()
print(cur.rowcount, "records inserted, last id:", cur.lastrowid)
6 records inserted, last id: 6
Explanation: %s placeholders pass values safely. commit() is required – without it the inserts are not saved.
Query
cur.execute("SELECT * FROM customers WHERE address LIKE %s ORDER BY name LIMIT 3", ("%o%",))
for row in cur.fetchall():
print(row)
(6, 'Atharv', 'Goa') (3, 'Keisha', 'Otawa 4') (4, 'Varun', 'Ontario')
Explanation: Addresses containing “o”, sorted by name, first 3 rows. fetchone() returns just the first row.
Update and Delete
cur.execute("UPDATE customers SET address = %s WHERE name = %s", ("Mohali", "Karan"))
cur.execute("DELETE FROM customers WHERE address = %s", ("Dublin 4",))
mydb.commit()
print(cur.rowcount, "record(s) deleted")
1 record(s) deleted
Joins
Join customers with an orders table on a proper foreign key (orders.customer_id = customers.id). INNER, LEFT and RIGHT joins and emulating FULL JOIN with UNION are covered step by step in Python MySQL Join.
customers.id = product.id, which matches unrelated rows that happen to share a number. Always join on a real relationship (a foreign key).Security Checklist
- Use placeholders (
%s) for every value – never build SQL with f-strings or +. - Keep passwords in environment variables, not in code.
- Give the app a MySQL user with only the privileges it needs.
- Store phone numbers as VARCHAR, not INT.
Python MySQL Series
- Python MySQL Installation
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert
- Python MySQL Select
- Python MySQL Where
- Python MySQL Order By
- Python MySQL Delete
- Python MySQL Drop Table
- Python MySQL Update
- Python MySQL Limit
- Python MySQL Join
Interview Questions
Q: Why is commit() needed?
mysql-connector starts a transaction; changes are saved only when committed.
Q: How do you prevent SQL injection?
Use parameterised queries with placeholders.
Practice Exercises
- Create a products table and insert 5 products with executemany.
- Write a function search_customers(city) using a placeholder.
Summary
Connect with mysql.connector, create tables, insert with executemany and commit, query with placeholders, and follow the series for each operation in detail.
Machine Learning Tutorials, Courses and Certifications