INTERFACE PROGRAMS -
STORE AND DISPLAY AN EMPLOYEE RECORD
AIM: To write a Python program to connect with database and to store an employee record and
display employee details.
SOURCE CODE:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table 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 ##")
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!!Thank you!!! ##")
else:
print("## INVALID CHOICE ##")
STORE AND UPDATE AN EMPLOYEE RECORD
AIM: To write a Python program to connect with database and to store an employee record and
update employee details.
INTERFACE PROGRAMS -
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table employee(empno int, name varchar(20), dept varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. UPDATE 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 ##")
elif choice==2:
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.fetchone()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT","%10s"%"SALARY")
print("%10s"%result[0],"%20s"%result[1],"%15s"%result[2],"%10s"%result[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) :")
elif choice==0:
con.close()
print("## Bye!!Thank you!!! ##")
else:
print("## INVALID CHOICE ##")
STORE AND SEARCH AN EMPLOYEE RECORD
AIM: To write a Python program to connect with database and to store an employee record and search
employee details.
SOURCE CODE:
import mysql.connector as mycon
con = mycon.connect(host='localhost', user='root', password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table employee(empno int, name varchar(20), dept varchar(20), salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. SEARCH 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 employee2 values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice==2:
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.fetchone()
if cur.rowcount==0:
print("Sorry! Empnumber not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT","%10s"%"SALARY")
print("%10s"%result[0],"%20s"%result[1],"%15s"%result[2],"%10s"%result[3])
ans=input("SEARCH MORE (Y/N) :")
elif choice==0:
con.close()
print("## Bye!!Thank you!!! ##")
else:
print("## INVALID CHOICE ##")
STORE AND DELETE AN EMPLOYEE RECORD
AIM: To write a Python program to connect with database and to store an employee record and delete an
employee details.
SOURCE CODE:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root', password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table employee(empno int, name varchar(20), dept varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. DELETE 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 ##")
elif choice==2:
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.fetchone()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT","%10s"%"SALARY")
print("%10s"%result[0],"%20s"%result[1],"%15s"%result[2],"%10s"%result[3])
choice=input("\n## ARE YOUR SURE TO DELETE ? (Y/N) :")
if choice.lower()=='y':
query="delete from employee where empno={}".format(eno)
cur.execute(query)
con.commit()
print("## RECORD DELETED ## ")
ans=input("DELETE MORE (Y/N) :")
elif choice==0:
con.close()
print("## Bye!!Thank you!!! ##")
else:
print("## INVALID CHOICE ##")