Ex13 23 24

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ex:13

Aim:

To write a program to connect python with Mysql using database


connectivity and perform the following operations on data in database:
Fetch,Update and delete the data.
(i) Create two tables
 Table name – Employee with
empno,Name,Desig,salary,Leave,Bonus
 Table name – Insurance with empno, LIC
(ii) Insert five records in both the tables by accepting the values of the
attributes from the user .
(iii) Display the total salary of each designation of those employees whose
name starts with “R”.
(iv) Display the employee number and Name who has the LIC insurance.
(v) Update the salary by 10% for those employees whose designation is
clerk.

import mysql.connector as sqltor


def displaydata(cursor):
datalist = cursor.fetchall()
for row in datalist:
print(row)
mycon=sqltor.connect(host="localhost",user="root",passwd="1234",database="
db1")
if mycon.is_connected()==False:
print('Error connecting to MySQL database')
cursor=mycon.cursor()
cursor.execute("create table employee(empno int,empname varchar(20),desig
varchar(20),salary float,leav int,bonus int)")
cursor.execute("create table insurance(empno int,lic varchar(10))")
cnt=1
while cnt<=5:
print("\n Enter the Employee details",cnt)
empno=int(input("Enter the employee number:"))
empname=input("Enter the employee name:")
desig=input("Enter the designation:")
salary=int(input("Enter the salary:"))
leave=int(input("Enter the leave taken:"))
bonus=int(input("Enter the bonus:"))
cursor.execute("insert into employee values
({},'{}','{}',{},{},{})".format(empno,empname,desig,salary,leave,bonus))
mycon.commit()
cnt+=1
cnt=1
while cnt<=5:
print("\nEnter the insurance details",cnt)
empno=int(input("Enter the employee number:"))
lic=input("LIC insurance (yes/no):")
cursor.execute("insert into insurance values({},'{}')".format(empno,lic))
mycon.commit()
cnt+=1
print("Selection of employee names whose name starts with R")
cursor.execute("select empname,sum(salary) from employee group by desig
having empname like '{}'".format('R%'))
displaydata(cursor)
print("Selection of Employee details who has LIC insurance")
cursor.execute("select employee.empno,empname,lic from employee ,insurance
where employee.empno=insurance.empno and lic='{}'".format('yes'))
displaydata(cursor)
mycon.commit()
print("starting updation of the table")
cursor.execute("update employee set salary=salary+salary*0.10 where
desig='clerk'")
mycon.commit()
print("The details of the table after updation")
cursor.execute("select * from employee")
displaydata(cursor)
mycon.commit()
mycon.close()

You might also like