MySQL Python Programs 1
MySQL Python Programs 1
1
Write a Python program that connects to a MySQL database, creates a stud table if it doesn't
exist, and allows the user to insert multiple student records (name, regno, section) into the table
and close the connection after committing the changes.
if connection.is_connected():
print("Connection established successfully!")
else:
print("Connection failed!")
cursor = connection.cursor()
# Inserting records
for i in range(n):
print("Enter the details of student no: "+str(i + 1))
# Closing resources
cursor.close()
connection.close()
print("Connection closed.")
Output:
Connection established successfully!
Enter the number of records: 3
Enter the details of student no: 1
Enter the name: Anil
Enter the regno: 100
Enter the section: 12a
Enter the details of student no: 2
Enter the name: Bharath
Enter the regno: 101
Enter the section: 12b
Enter the details of student no: 3
Enter the name: Charan
Enter the regno: 102
Enter the section: 12e
Data successfully added to the table.
Connection closed.
PROGRAM NO. 2
Write a Python program that connects to a MySQL database, creates a table, inserts multiple
rows based on user input, adds a new column for "gender", and updates the column values
accordingly.
if connection.is_connected():
print("Connection established successfully!")
else:
print("Connection failed")
cursor = connection.cursor()
# Inserting records
for i in range(n):
print("Enter the details of student no: "+str(i + 1))
name = input("Enter the name: ")
regno = int(input("Enter the regno: "))
section = input("Enter the section: ")
Output:
Connection established successfully!
Enter the number of records: 3
Enter the details of student no: 1
Enter the name: Bhanu
Enter the regno: 10
Enter the section: 9a
Enter the details of student no: 2
Enter the name: Rana
Enter the regno: 11
Enter the section: 9b
Enter the details of student no: 3
Enter the name: Chandru
Enter the regno: 15
Enter the section: 9e
Data successfully added to the table.
Enter the gender of Bhanu: Female
Enter the gender of Rana: Male
Enter the gender of Chandru: Male
Data successfully updated in the table.
PROGRAM NO. 3
Write a Python program to connect to a MySQL database, display all records from a specified
table, and allow the user to delete a record based on a given identifier (e.g., regno or name).
if connection.is_connected():
print("Connection established successfully!")
else:
print("Connection Failed")
cursor = connection.cursor()
if result:
# Deleting the record using parameterized query
delete_query = "DELETE FROM emp WHERE id = %s;"
cursor.execute(delete_query, (reg,))
Output:
Connection established successfully!
Records of employee table are:
Name ID DeptID Salary
Alice 101 1 50000.00
Bob 102 2 60000.00
Charlie 103 1 70000.00
David 104 3 55000.00
Eve 105 2 75000.00
if connection.is_connected():
print("Connection established successfully!")
else:
print("Connection Failed")
cursor = connection.cursor()
if existing_record:
# Updating the record using parameterized query
update_query = "UPDATE emplo SET qualification = %s WHERE empno = %s;"
cursor.execute(update_query, (newQ, eno))
connection.commit()
print("Record updated successfully.")
# Fetching and displaying the updated records
cursor.execute("SELECT * FROM emplo;")
data = cursor.fetchall()
Output:
Connection established successfully!
Records of employees are:
EmpNo. Name DOB Qualification
101 John 1990-05-15 B.Sc
102 Alice 1985-03-20 M.Sc
103 Bob 1987-11-10 Ph.D
104 David 1992-07-25 B.Com
105 Eva 1989-12-30 MBA