Practical Questions Mysql For Record 2023-24
Practical Questions Mysql For Record 2023-24
Q1.
Q3: Program to connect with database and insert record of employee and display
records.
Solution:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="kv2uppal")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table if not exists employee(empno int, name varchar(20), dept
varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. DISPLAY RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print(" Data Saved Successfully")
elif choice == 2:
query="select * from employee" cur.execute(query)
result = cur.fetchall()
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT",
"%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
elif choice==0:
con.close()
print("Bye!!.....Thankyou ")
else:
print(" INVALID CHOICE ")
Q4: Program to connect with database and search employee number in table employee
and display record, if empno not found display appropriate message.
Solution:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="kv2uppal",
database="company")
cur = con.cursor()
print("EMPLOYEE SEARCHING FORM")
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO SEARCH :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT",
"%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
ans=input("SEARCH MORE (Y) :")
Q5: Program to connect with database and update the employee record of entered
empno.
Solution:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="kv2uppal",
database="company")
cur = con.cursor() print("#"*40)
print("EMPLOYEE UPDATION FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO UPDATE :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT",
"%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
choice=input("\n ARE YOUR SURE TO UPDATE ? (Y) :")
if choice.lower()=='y':
print(" YOU CAN UPDATE ONLY DEPT AND SALARY")
d = input("ENTER NEW DEPARTMENT "))
s = int(input("ENTER NEW SALARY,( "))
query="update employee set dept='{}',salary={} where
empno={}".format(d,s,eno)
cur.execute(query)
con.commit()
print(" RECORD UPDATED ")
ans=input("UPDATE MORE (Y) :")
Q6: Program to connect with database and delete the record of entered employee
number.
Solution:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="kv2uppal",
database="company")
cur = con.cursor()
print("EMPLOYEE DELETION FORM")
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO DELETE :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME",
"%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
choice=input("\n ARE YOUR SURE TO DELETE ? (Y) :")
if choice.lower()=='y':
query="delete from employee where empno={}".format(eno)
cur.execute(query)
con.commit()
print(" RECORD DELETED SUCCESSFULLY! ")
ans=input("DELETE MORE ? (Y) :")