For Installation MYSQL Connector Run This Command:
pip install mysql-connector-python
# Import the MySQL connector library to connect Python with MySQL
import mysql.connector as mysql
# Establish a connection to the MySQL database
# Host where the database server is running (localhost means your own computer)
# Username to access the database
# Password for the database user
# Name of the database to use
mydb = mysql.connect(
host="localhost",
user="root",
password="54321",
database="banks"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM employee")
myresult = mycursor.fetchall()
for x in myresult:
print(x) # Print each record
# SQL query to insert a new record into 'employee' tablesql = "INSERT INTO employee VALUES (%s, %s,%s,%s,%s)"
# Values to insert (id, name, job, date, salary)
val = (7, "Raju","Doctor",'2025-07-01','20')
# Execute the insert query with the provided values mycursor.execute(sql, val)
# Commit the transaction to save changes to the database mydb.commit()
# Print how many records were inserted print(mycursor.rowcount, "record inserted.")