Tuesday , December 1 2026
Python MySQL Database

Python MySQL Database

Updated: 28 November 2026Level: BeginnerReading time: about 10 minutesmysql-connector-python

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

Install
pip install mysql-connector-python
Example
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)
Output
('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.

The itronix database in phpMyAdmin
The itronix database in phpMyAdmin

Create a Table and Insert Rows

Example
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)
Output
6 records inserted, last id: 6

Explanation: %s placeholders pass values safely. commit() is required – without it the inserts are not saved.

Query

Example
cur.execute("SELECT * FROM customers WHERE address LIKE %s ORDER BY name LIMIT 3", ("%o%",))
for row in cur.fetchall():
    print(row)
Output
(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

Example
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")
Output
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.

Note: The earlier version of this page joined 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

  1. Python MySQL Installation
  2. Python MySQL Create Database
  3. Python MySQL Create Table
  4. Python MySQL Insert
  5. Python MySQL Select
  6. Python MySQL Where
  7. Python MySQL Order By
  8. Python MySQL Delete
  9. Python MySQL Drop Table
  10. Python MySQL Update
  11. Python MySQL Limit
  12. 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

  1. Create a products table and insert 5 products with executemany.
  2. 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.

Next: Python MySQL Installation

About Machine Learning

Check Also

Microsoft Native & Display Advertising Certification Exam Answers

Microsoft Advertising Native & Display Certification Exam Answers 2022 – 100% Correct Question 1 : …