Term 2 CSPractical File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

ST.

JOSEPH’S CONVENT HIGH SCHOOL


(C.B.S.E) CHITTARANJAN
SESSION:-2021-22

COMPUTER SCIENCE
SUB. CODE - 083
TERM-II PRACTICAL FILE

NAME:_ _

STREAM:_ _

BOARD ROLL NO:- _ _


Certificate

This is to certify that ,

Roll No: , of Class:XII ,Session :2021-22

has prepared the Practical file as per the prescribed


syllabus of Term-II

COMPUTER SCIENCE (SUB. CODE:-083)

under my supervision, I am completely satisfied by


the performance. I wish him/her all the success in
life.

Principal’s Signature Subject Teacher’s Signature

External’s Signature
TERM-II INDEX PAGE
Section- A:- Python
PROGRAMS DATE SIGN.
Program 1:- Write a menu based program to perform the operation on stack in python.

Program 2:- Write a menu based program to Maintaining Book details like bcode, btitle
and price using stacks in python.
Program 3:- Write a menu based program to add, delete and display the record of
hostel using list as stack data structure in python. Record of hostel contains the fields :
Hostel
number, Total Students and Total Rooms.

Section-B :- Mysql
PROGRAMS DATE SIGN.
1. Write SQL query to create a database Employee.
2. Write SQL query to open database Employee.
3. Write SQL query to create following Table name empl.

4. Write SQL query to show the structure of table.


5. Write SQL query to Insert 10 records same as it is in image.
6. Write SQL query to display all the records from table empl.
7. Write SQL query to display EmpNo and EName of all employees from the table
empl.
8. Write SQL query to display Ename, Sal, and Sal added with comm from table empl.
9. Write SQL query to display Ename, Sal, and deptno who are not getting
commission from table empl.
10. Write SQL query to display Eno, Ename ,Sal, and Sal*12 as Annual Salary
whose commission is not NULL from table
11. Write SQL query to display the details of employees whose name have only
four letters.
12. Write SQL query to display name, job title and salary of employee who do not
have manager.
13. Write SQL query to display the name of employee whose name contains “A” as
third letter.
14. Write SQL query to display the name of departments. Each department
should be displayed once (DISTINCT).
15. Write SQL query to display the name and salary of those employees whose
salary is between 35000 and 40000. (BETWEEN)
16. Write SQL query to display tables data according to ascending order of sal.
17. Write SQL query to change EName MITA by MIRA.
18. Write SQL query to delete records whose deptno=10.
19. Write SQL query to add a new column Phno.
20. Write SQL query to delete entire table IF EXISTING.
PROGRAMS DATE SIGN.

21. Queries for Aggregate functions- SUM( ), AVG( ), MIN( ), MAX( ), COUNT( ) .
22. TEXT FUNCTIONS ( TRIM FUNCTION, SUBSTR FUNCTION,
LEFT FUNCTION, RIGHT FUNCTION , MID FUNCTION )
23. MATH FUNCTIONS ( POWER (), ROUND (), MOD () ).
24. DATE FUNCTIONS NOW (), DATE (), MONTH (), MONTHNAME (), YEAR (),
DAY (), DAYNAME ()
25. Querying and manipulating data using Group by, Having, Order by.
26. Create a table DEPT and show Cartesian Product, JOIN
(Cartesian Join, Equi Join, Natural Join)

Section - C (Python MySql Connectivity)

PROGRAMS DATE SIGN.


27. Program to connect with database and store record of employee and display
records. 28: Program to connect with database and update the employee record of
entered empno.
29. Program to connect with database and search employee number in table employee
and display record, if empno not found display appropriate message.
30. Program to connect with database and delete the employee record of entered
empno.

*********************************
Section-A ( PYTHON)
Program 1:- Write a menu based program to perform the operation on stack in python.
Source Code:-

def isEmpty(S):
if len(S)==0:
return True
else:
return False

def Push(S,item):
S.append(item)
top=len(S)-1

def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = S.pop()
if len(S)==0:
top=None
else:
top=len(S)-1
return val

def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top=len(S)-1
return S[top]

def Show(S):
if isEmpty(S):
print("Sorry No items in Stack ")
else:
t = len(S)-1
print("(Top)",end=' ')
while(t>=0):
print(S[t],"<==",end=' ')
t-=1
print()

# main begins here


S=[] #Stack
top=None
while True:
print("**** STACK DEMONSTRATION ******")
print("1. PUSH ")
print("2. POP")
print("3. PEEK")
print("4. SHOW STACK ")
print("0. EXIT")
ch = int(input("Enter your choice :"))
if ch==1:
val = int(input("Enter Item to Push :"))
Push(S,val)
elif ch==2:
val = Pop(S)
if val=="Underflow":
print("Stack is Empty")
else:
print("\nDeleted Item was :",val)
elif ch==3:
val = Peek(S)
if val=="Underflow":
print("Stack Empty")
else:
print("Top Item :",val)
elif ch==4:
Show(S)
elif ch==0:
print("Bye")
break
Program 2:- Write a menu based program to Maintaining Book details like bcode, btitle and
price using stacks in python.
Source Code:-

book=[]
def push():
bcode=input("Enter bcode:- ")
btitle=input("Enter btitle:- ")
price=input("Enter price:- ")
bk=(bcode,btitle,price)
book.append(bk)
def pop():
if(book==[]):
print("Underflow / Book Stack in empty")
else:
bcode,btitle,price=book.pop()
print("poped element is ")
print("bcode:- ",bcode," btitle:- ",btitle," price:- ",price)
def traverse():
if not (book==[]):
n=len(book)
for i in range(n-1,-1,-1):
print(book[i])
else:
print("Empty , No book to display")
while True:
print("Book Stall")
print("*"*40)
print("1. Push")
print("2. Pop")
print("3. Traversal")
print("4. Exit")
ch=int(input("Enter your choice:- "))
if(ch==1):
push()
elif(ch==2):
pop()
elif(ch==3):
traverse()
elif(ch==4):
print("End")
break
else:
print("Invalid choice")
File Edft Shell Debug Options Window Help

RESTART : C : /Pychon36—32/book.py
Book Stall

1. Push
2. Pop
3. Traversal
Q. Exit
Enter your choice:-
1 Enter bcode:-
101
Enter btit1e:- CS
Enter price:- Q00
Book Stall

2. Pop
3. Traversal
9. Exit
Enter your choice:-
1 Enter bcode:-
2
Enter btitle:- IP
Enter price:- 400
Book Stall

1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice:-
3 ('2', 'IP', 'QOO')
‹•ioi', •cs, oo)
Book Stall

1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice:- 2
poped element is
bcode:- 2 btit1e:- IP price:- 400
Program 3:- Write a menu based program to add, delete and display the record of hostel using
list as stack data structure in python. Record of hostel contains the fields : Hostel number,
Total Students and Total Rooms.
Source Code:-
host=[ ]
ch='y'
def push(host):
hn=int(input("Enter hostel number:-"))
ts=int(input("Enter Total students:-"))
tr=int(input("Enter total rooms:-"))
temp=[hn,ts,tr]
host.append(temp)

def pop(host):
if(host==[]):
print("No Record")
else:
print("Deleted Record is :",host.pop())

def display(host):
l=len(host)
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])

while(ch=='y' or ch=='Y'):
print("St Joseph's Convent Hostel")
print("*"*40)
print("\n")
print("1. Add Record")
print("2. Delete Record")
print("3. Display Record")
print("4. Exit")
op=int(input("Enter the Choice:-"))
if(op==1):
push(host)
elif(op==2):
pop(host)
elif(op==3):
display(host)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N)")
File Edit Shell Debug Options Window Help

===«===«==«====== \ \; C• /P yt hOW 3 b-32 ,py ==«==«===«===«===


5c Joseph's Convent Hostel

1 . Aclcl Reco rd
2 . De l e c e Re co ra
3 . Di up la y Re cor a
4. Exit
Enter the Choice:-1
Enter hostel nunber:-
101 Enter Total
students:-39 Enter
Total roows:-20
Do you wanc to enter more(Y/N)7
Sc Joseph’s Convent Rostel

1. Add Recora
2. Delete Record
3. Display Record

Enter c•ie Cño xce : -1


Encer nos eel uncler : -102
Encer To c a1 s c uaencs : - s o
Enter total roowe:-20
Do you want to enter more(Y/N)Y
St Joseph's Convent Hostel

1. Add Record
2. Delete Recora
3. Display Recora
Section-B ( MYSQL )
1. Write SQL query to create and show a database Employee.

2. Write SQL query to open database Employee.

3. Write SQL query to create following Table name empl.


create table empl(empno int(4), ename char(20),job char(10), mgr int(4), hiredatedate,sal
int(4), comm int(3), deptno int(2));
4. Write SQL query to show the structure of table.

5. Write SQL query to Insert 10 records same as it is in image.

Write Insert Query for 10 times.


6. Write SQL query to display all the records from table
empl. Ans: select * from empl;
7. Write SQL query to display EmpNo and EName of all employees from the table
empl. Ans: select EmpNo, EName from empl;

8. Write SQL query to display Ename, Sal, and Sal added with comm from table
empl. Ans: select ename,sal,sal+comm from empl;

9. Write SQL query to display Ename, Sal, and deptno who are not getting commission from
table Ans:- select ename,sal,deptno from empl where comm IS NULL;

10. Write SQL query to display Eno, Ename ,Sal, and Sal*12 as Annual Salary whose commission is not
NULL from table empl.
Ans:- select empno,ename,sal,sal*12 "Annal Salary" from empl where comm IS NOT NULL;
11. Display the details of employees whose name have only four
letters. Ans: select * from empl where EName like “ ‟;

12. Display name, job title and salary of employee who do not have
manager. Ans: select EName, job, sal from empl where mgr =
NULL;

13. Display the name of employee whose name contains “A”as fourth
letter. Ans: select EName from empl where EName like “_ _ _A
%”

14. Display the name of departments. Each department should be displayed once.
Ans: SELECT DISTINCT(Dept) FROM EMPLOYEE;

15. Display the details of all employee whose annual salary is between 25000 to
40000.Ans: select * from empl where sal between 25000 and 40000;
16. Write SQL query to display tables data according to ascending order of
sal. Ans: select * from empl order by sal asc;

17. Write SQL query to change EName MITA by MIRA.


Ans:- update empl set ename="MIRA" where ename="MITA";

18. Write SQL query to delete records whose


comm=0. Ans:- delete from empl where
comm=0;
19. Write SQL query to add a new
column Phno. Ans:- alter table empl add
phno int(10);

20. Write SQL query to delete entire table IF


EXISTING. Ans:- drop table IF EXIST empl;

***********************
21. Queries for Aggregate functions- SUM( ), AVG( ), MIN( ), MAX( ), COUNT( )
a. Display total salary of all employee listed in empl.

b. Find the average salary of the employees in empl table.


Solution:- SELECT avg(salary) FROM EMPL;

c.Find the minimum salary of job SALEMAN in EMPL table.


Solution:- SELECT min(sal) FROM EMPL WHERE

job=’SALESMAN’;
d. Find the maximum salary of job SALEMAN in EMPL
table. Solution:- SELECT max(sal) FROM EMPL WHERE
job=’SALESMAN’;

e. Find the total number of Employee from empl table.


Solution:- SELECT count(empno) from empl;
22. TEXT FUNCTIONS
Display name of employee in lower case and upper case;
mysq17 se 1ect lease Rename 7 From emp1 i mysq17 se feet ucase Rename fi £z am empl i
+——————————————+ +—————————————— +
i 1case Rename 3 i ucase Rename 3
i smith i i SI4ITH i
i anya i i AHVA i
i seth i i SETH i
i mahade u an i i HfiHADEU8H i
i mom in i I t40fH H I
I b1na I i BINfl i
i am1z• i I flNIR I
i sh1aunash i i SHIRUNRSH i
i anoop i i RNOOP i
s c ot I I i SCOTT
jat in i i JflT I h1
m1ra i ; I4IpQ
13 rous 1n set GB .B3 sec 7 13 a•ous
in set GB .BB sec 7

Display the position of the string ‘LE’ in field TRIM FUNCTION


mysql> select TRIM(" MYSQL ");
job of table EMPL.
mysql› select job,instr(job,'le') as “LE in lob“from empl; TRIN " NYSQL ")
+ - -- - -- -- - -- -- - - - -- - - -+
j 0b | LE In Job NYSQL

CLERK | 2 l row in set (O.OO secJ


I SALESNAh | 3
SALESkAh | 3
MANAGER | 0
I SALESNAh | 3
'MANAGER | 0
MANAGER | 0
ANALYST | 0 1 row in set (0.00 sec)
I PRESIDENT | 0
I SALESMAN | 3 mysql> select RTRIM(” MYSQL
CLERK | 2
l cleark | 2 RTRIN " NYSQL ") I
l Cleark | 2
MYSQL I
13 rows in set (0. 31 sec)
1 row in set (O.OO sec)

SUBSTR FUNCTION LENGTH FUNCTION


mysq1> select substr('ABCDEF',3,4)”Subs"; mysq1> select LENGTH(’ABCDEF')”LENGTH OF STRING”

SUbs I LENGTH OF STRING

CDEF ) 6
+------+ /I row 1 n set {0. 00 s ec)
1 row in set (0.00
sec)

LEFT FUNCTION RIGHT FUNCTION MID FUNCTION


my_s+ql> select 3 mysql> select RIGHT('ABCDEF',3); mysql> select MID('ABCDEF'
LEFT('ABCDEF' ,3,4);
LEFT ’ ABCDE F ' , 3) RIGHT('ABCDEF',3) ) NID ( ' ABCDE F ’ , 3, 4)
I ABC OE F CDEF
1 row in set (0.00 sec) 1 row In set {0. 01 sec} 1 row 1n set {0. 00 sec)

23. MATH FUNCTIONS

MOD Function Power Function Round Function


mysql> select MoD(15,4)”Modules"; mysql> select POw(2,3J"Power”; mysql> select ROUND(15.67,1)”ROUND";
---------+ +-------+ +-------+
Modules | | Power ) | ROUND
---------+ +-------+ +-------+
3 l | g | | 15.7
---------+ +-------+ + -------+
1 row in set (0.08 sec) 1 row in set (0.62 secJ 1 row n set (0.3O secJ

Square Root Function


mysq i > s e i ect s qrt ts4) ” square Root ” ;
l square Root l
8
I row 1 n s et (0 . 00 sec)
24. DATE / TIME FUNCTION

25. Querying and manipulating data using Group by, Having, Order by.
a) Display department no, job and no. of employee in particular job Group by department
no, job from empl.

b) Display the jobs where the number of employee is less than 3.


26. Create a table DEPT and show Cartesian Product, JOIN (Cartesian Join, Equi Join,
Natural Join)

i NUbb i 2B i

14 yous xn seI GB . 34 sec 7

I depcno I dneu»e • Doc


i IN i ACC08lfl I HU i ttEU DEt›tl I
38 i SALES i li0LttA TA

6 rows In seI £ B. 06 sec 7

a) Cartesian Product

mys q17 se fee t From empl, depC i


l
I empno i ename " i ,job " " i mgs " i h 1x'edaC e l sal I comm l deptno l deptno l dname l loc
›° 8369 ! SNtTH ! CI.ERX ! 8982 i 1998—12—18 i 888 ! NULL i 28 i 18 i ACCOUNT I NG i NSU DELH I
i 8369 ! SNITH ! CLERX ! 8902 I 1990—12—18 i 800 ! NULL I 2B I 2B I RES E9RCH i CHENNE I
i 8369 ! SNITH ! CbERX ! 8902 i 1990—12—18 i 8B0 i NULL i 2B i 3B i S9LES I XObXR T 9
i 8369 ! SNITH ! CbERX ! 8902 i 199B—12—18
H ›° 8B0 ! NULL i 2B i SB i N9RXET I NC i B9NCLORE
t C B
i 16BB ’ L 2a i ie i acco NT NG i NEW E5 I
i 16BB i 3BB i 3B i 2B i RESEARCH i CHENNE I
| | I 160B i 3B8 i 38 i 38 i SA LE8 i itOLitflT A
it, :l: I It l .
tt ,'9’’’’- I

2
I 85t . tt,H

i 8521 ! SETH
8521 ! SETH
! RL| t ,. |
! SRLESNRN
! SRLESNRN
t, „ , |„|, I,
! 8698 i 1991-B2—22 i 125B !
! 8698 i 1991-B2-22 i 125I
,II. /t*t* I ). I;,sl(t'/!'l’l(:i)it’
SBB i
i SBB i
3B
3B
i
i
SB i NRRXET I NG ' BfINGI.ORE
SB i NRRXET I NG i BfiNGI.ORE
° 8566 i NRHRDEURN i NRNRGER i 8839 i 1991-€I4-€I2 i 2985 i NULI. ' 2€I i 1€I ' ACCOUNT I NG i N£tJ DELH I
' 8566 I dRHRDEURN I dRNRGER 1 8839 ' 1991-€I4-€I2 ' 2985 I NULI• ' 2€I ' 2€I ' RESEARCH ' CHENNEI
I 8S6b .' dRHRDEURN i dRNRCER i 8839 ' 1991-84-IB2 i 2985 i NUI.b i 28 ' 38 i SRI.ES i X06XR SR
I 8S66 .' dRHRDEURN ! dRNRCER ! 8839 i 1991-84-82
' 8566 ! NRHRDEURN ! I1RNRCER ! 8839 i 1991-84-82
, 8566, | NRHRDEURN | NRNRCER„ | 8839 , 1991-84-82 , t„, . A, , , ,. , NQRX ,/t, , „NCLOR
II

! 8886 i 9HO0P ! CbERX ! 8888 i 1993—B1—12


! 8888 i SCOTT ! 9N9LYST ! 8566 i 1992'12'B9
i NUI•L ! 2B i SB i NfIRXETING ! BRNGLORE i
3 e i NUI•L i 2€I i 50 i NfIRXEIlNG i BRNGLORE i
3 ee i NUI•L i
8 2€I i 50 i NfIRXETlNG i BRNGLORE i
e e NUI•L i 3€I i 10 i RCC0UN7 I NG i NE\J DEI.H I
3eee
sse
958 i HULL I 3B I 2B I RE8 EA BCH I CHEHHE I
! 890B ›° JRTIN ! CbERX ! 8698 i i 1991—12—B3
1991—12—B3
! 890B ›° JRTIN ! CbERX ! 8698
! 890B ›° J9T ! CbERX ! 8698 i 199 '12'B3
i 3B00 i U£.L ! : I'' E HT HG ! ’DEI.II
• 89B2 i PfiXTR • fINRI.YST • 8566 i 1991—12—€l3 i 3BB0 ' NUI•L ! 2B i 2B i RESEfIRCH ! CHENNET
i 89B2 ' PfiXTR i fINRI.YST i 8566 i 1991—12—€l3 i 3BB0 ' NUI•L ! 2B i 3B i SfII,ES ! XOLXRTR
i 89B2 ' PfiXTR i fINRI.YST i 8566 i 1991—12—€l3 i 3BB0 ' NUI•L ! 2B i SB i NfIRXETlNG ! BRNGLORE
i 89B2 ' PfiXTR i fINRI.YST i 8566 i 1991—12—€l3 i 3BB0 ' NUI•L ! 2B i SB i NfIRXETlNG ! BRNGLORE
t 89B2 l PAXIR T ANALYST l 8566 l *991-*2—03 1 3000 i NUI,L 1 20 ' S0 i NRRXETING ! BfING£ORE
8934 l NITA ' CLERW ! 8882 l L992-01-23 i 130id i HULL ! 1£i i UI i A CCOUi'4T I HG i HEtf DELH I
8934 ' NIIN I CI.£RX •' 8882 ' 1992—B1—23 1380 1 HULL ' i8 ! 28 ' RESEARCH ! CWEWNEO
8934 ' NO IR I CI.ERX 1 8882 . 1992—01—23 130B . NULL I 1B ' 30 ' SRI•£S I X0I.Xfi1fi
8934 t ñ1TA ! CLEDX ! 8882 ! 1992-01-23 1 38£i 1 HULL ! 1 ! SP i HâRHET I UG 1 B8F1GLORE
8934 . NT Tfl . C6ERX . 8882 . 1992—01—23 13B€I . NULL . 1B. SB NflRXET I NG . BflNCLORE
8934 . NT Tfl . C6ERX . 8882 . 1992—01—23 13B€I . NULL . 1B. SB NflRXET I NG . BflNCLORE
84 z‘ows 1n set £ B. BB sec 7
b) Write query to join two tables empl and dept on the basis of deptno field. (Equi join)

c) Natural Join

d) Left Join

e) Right join
27: Program to connect with database and store record of employee and
display records.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root")
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 ##")
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!! ##")
else:
print("## INVALID CHOICE ##")
28: Program to connect with database and update the employeerecord of entered
empno.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root", 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 ==")
print("== FOR EMPNO AND NAME CONTACT ADMIN ==")
d = input("ENTER NEW DEPARTMENT,(LEAVE BLANK IF NOT WANT TO CHANGE )")
if d=="":
d=row[2]
try:
s = int(input("ENTER NEW SALARY,(LEAVE BLANK IF NOT WANT TO CHANGE ) "))
except:
s=row[3]
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) :")
29. Program to connect with database and search employee number in table
employee and display record, if empno not found display appropriate message.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root", database="company")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE SEARCHING FORM")
print("#"*40)
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) :")
30. Program to connect with database and delete the employeerecord of entered
empno. Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root", database="company")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE DELETION FORM")
print("#"*40)
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) :")

*********************************

You might also like