๐Ÿ‡ฎ๐Ÿ‡ณ India's Free Coding Tutorial โ€” Learn C, PHP, MySQL, Python, Java About ยท Contact
Advertisement

MySQL with Python

Python Python for Web & Data ๐Ÿ“… Mar 2026 โฑ 5 min read ๐Ÿ†“ Free

Connect Python to MySQL

bash
pip install pymysql
python
import pymysql

# Connect
conn = pymysql.connect(
    host="localhost",
    user="root",
    password="",
    database="school",
    charset="utf8mb4"
)
cursor = conn.cursor(pymysql.cursors.DictCursor)

# INSERT
cursor.execute(
    "INSERT INTO students (name, marks) VALUES (%s, %s)",
    ("Gagan", 95)
)
conn.commit()

# SELECT
cursor.execute("SELECT * FROM students ORDER BY marks DESC")
rows = cursor.fetchall()
for row in rows:
    print(f"{row['name']}: {row['marks']}")

# Close
cursor.close()
conn.close()
Advertisement
โ† Back to Python Index