Questions on Mysql-dbms and Python Connectivity With Answers Using Format()
Questions on Mysql-dbms and Python Connectivity With Answers Using Format()
format()
Q1. Write a Python program to insert a new employee’s record into the Employee table. Take
Cname (name of the employee) and CId (ID of the employee) as inputs from the user.
Answer:
import mysql.connector
cursor = mydb.cursor()
# Commit changes
mydb.commit()
Q2. Write a Python program to update the name of an employee in the Employee table
based on their CId. Take inputs for CId and the new Cname from the user.
Answer:
import mysql.connector
# Commit changes
mydb.commit()
---
Q3. Write a Python program to delete an employee's record from the Employee table using
their CId. Take CId as input from the user.
Answer:
import mysql.connector
cursor = mydb.cursor()
# Commit changes
mydb.commit()
Q4. Write a Python program to insert multiple employee records into the Employee table
using user inputs.
Answer:
import mysql.connector
cursor = mydb.cursor()
for _ in range(n):
Cname = input("Enter Employee Name: ")
CId = int(input("Enter Employee ID: "))
query = "INSERT INTO Employee (Cname, CId) VALUES ('{}', {})".format(Cname, CId)
cursor.execute(query)
# Commit changes
mydb.commit()