Computer Science
Computer Science
Computer Science
NO. DESCRIPTION
1. List of libraries
2. List of functions
3. About the project
4. Code
5. Output
List of libraries
1. mysql.connector mysql.connector is used for
connecting to a MySQL
database server from Python.
List of Functions
No Functions Uses
Sets
1 up
connect()
a connection, establishing a session with MySql
server.
2 cursor() Used to execute SQL commands.
3 execute() Used to run queries in MySQL and get
the result back in Python.
4 commit() Used to save data in the database.
5 close() Transfers data from buffer memory to a
file and closes the file.
Code
import mysql.connector as mycon
cn =
mycon.connect(host='localhost',user='root',password
="root",database="company")
cur = cn.cursor()
def showAll():
global cn
global cur
try:
query="select * from emp"
cur.execute(query)
results = cur.fetchall()
print("-"*45)
print('%5s'%"EMPNO",'%15s'%'EMP
NAME','%12s'%'DEPARTMENT','%10s'%'SALARY')
print("-"*45)
count=0
for row in results:
print('%5s' %
row[0],'%15s'%row[1],'%12s'%row[2],'%10s'%row[3])
count+=1
print("-"*10,"TOTAL RECORD :",count,"-"*18)
except:
print("error")
def addEmp():
global cn,cur
print("-"*10,"ADD NEW EMPLOYEE","-"*10)
eno = int(input("Enter employee number :"))
en = input("Enter employee name :")
dp = input("Enter department ")
sl = int(input("Enter Salary :"))
query="insert into emp
values("+str(eno)+",'"+en+"','"+dp+"',"+str(sl)+")"
cur.execute(query)
cn.commit()
#print(query)
print("RECORD ADDED SUCCESSFULLY!")
def searchEmp():
global cn,cur
print("-"*10,"SEARCH EMPLOYEE FORM","-"*10)
en = int(input("Enter Employee number to search
:"))
query="select * from emp where eno="+str(en)
cur.execute(query)
results = cur.fetchall()
if cur.rowcount<=0:
print("SORRY! NO MATCHING DETAILS
AVAILABLE")
else:
print("-"*45)
print('%5s'%"EMPNO",'%15s'%'EMP
NAME','%12s'%'DEPARTMENT','%10s'%'SALARY')
print("-"*45)
for row in results:
print('%5s' %
row[0],'%15s'%row[1],'%12s'%row[2],'%10s'%row[3])
print("-"*50)
def editEmp():
global cn,cur
print("-"*10,"EDIT EMPLOYEE FORM","-"*10)
en = int(input("Enter Employee number to edit :"))
query="select * from emp where eno="+str(en)
cur.execute(query)
results = cur.fetchall()
if cur.rowcount<=0:
print("SORRY! NO MATCHING DETAILS
AVAILABLE")
else:
print("-"*45)
print('%5s'%"EMPNO",'%15s'%'EMP
NAME','%12s'%'DEPARTMENT','%10s'%'SALARY')
print("-"*45)
for row in results:
print('%5s' %
row[0],'%15s'%row[1],'%12s'%row[2],'%10s'%row[3])
print("-"*50)
ans = input("Are you sure to update ? (y/n)")
if ans=="y" or ans=="Y":
d = input("Enter new department to update
(enter old value if not to update) :")
s = int(input("Enter new salary to update (enter
old value if not to update) :"))
query="update emp set dp='"+d+"',sl="+str(s) + "
where eno="+str(en)
cur.execute(query)
cn.commit()
print("RECORD UPDATED!")
def delEmp():
global cn,cur
print("-"*10,"DELETE EMPLOYEE FORM","-"*10)
en = int(input("Enter Employee number to delete
:"))
query="select * from emp where eno="+str(en)
cur.execute(query)
results = cur.fetchall()
if cur.rowcount<=0:
print("SORRY! NO MATCHING DETAILS
AVAILABLE!")
else:
print("-"*45)
print('%5s'%"EMPNO",'%15s'%'EMP
NAME','%12s'%'DEPARTMENT','%10s'%'SALARY')
print("-"*45)
for row in results:
print('%5s' %
row[0],'%15s'%row[1],'%12s'%row[2],'%10s'%row[3])
print("-"*50)
ans = input("Are you sure to delete ? (y/n)")
if ans=="y" or ans=="Y":
query="delete from emp where eno="+str(en)
cur.execute(query)
cn.commit()
print("RECORD DELETED!")
def clear():
for i in range(1,50):
print()
def generateSlip():
global cn,cur
print("-"*10,"SALARY SLIP","-"*10)
en = int(input("Enter Employee number to print
salary slip :"))
query="select * from emp where eno="+str(en)
cur.execute(query)
results = cur.fetchone()
if cur.rowcount<=0:
print("SORRY! NO MATCHING DETAILS
AVAILABLE!")
else:
clear()
print("EMPNO :",results[0]," "*20,"NAME
:",results[1])
print("DEPARTMENT :",results[2])
print("-"*50)
s = int(results[3])
hra = s * 12/100
da = s * 15/100
it = 1000
nps = (s+hra)*10/100
gross = s +hra+da+nps
ded = it + nps
net = gross - ded
tded=it + nps
print("%19s"%"EARNING","%27s"%"DEDUCTION")
print("-------------------------------------------------")
print("%20s"%"Basic :"+str(s),"%20s"%"INC. TAX
:"+str(it))
print("%20s"%"HRA :"+str(hra),"%20s"%"NPS
:"+str(nps))
print("%20s"%"DA :"+str(da))
print("%20s"%"NPS :"+str(nps))
print("-"*70)
print(" GROSS :",gross," NET SALARY :",net,"
TOTAL DED :",tded)
print("-"*70)
print("=== PRESS ANY KEY ===")
input()
def abc():
print("1. SHOW EMPLOYEE LIST ")
print("2. ADD NEW EMPLOYEE")
print("3. SEARCH EMPLOYEE ")
print("4. EDIT EMPLOYEE ")
print("5. DELETE EMPLOYEE ")
print("6. GENERATE PAY SLIP ")
print("7. EXIT")
ans = int(input("Enter your choice :"))
if ans==1:
showAll()
elif ans==2:
addEmp()
elif ans==3:
searchEmp()
elif ans==4:
editEmp()
elif ans==5:
delEmp()
elif ans==6:
generateSlip()
elif ans==7:
print("\nBye!!")
cn.close()
def pqr():
print("1. Contact us")
print("2. Enter your emp no. to see your info ")
print("3. EXIT")
an=int(input("enter ur option:"))
if an==1:
print("-"*60)
print(" "*20,"AUTHOR : RIYA KUNDANANI ")
print(" "*20,"EMAIL :
[email protected]")
print("-"*60)
elif an==2:
searchEmp()
elif an==3:
print("byee")
while True:
print("-"*15)
print("1. Admin menu")
print("2. user menu")
print("3. Exit")
print("-"*15)
answer=int(input("choose ur option:"))
if answer==1:
R=input("Enter password:")
if R=="Riya1234":
abc()
else:
print("Incorrect password")
elif answer==2:
pqr()
elif answer==3:
print("Bye!!")
break
Output