Professional Documents
Culture Documents
2
Exp.NO:1 Date:
Aim:Create Table Garment and Fabric -Python -Sql Connectivity. Insert Records, delete
record of an with fcode. Modify the price of any fcode
Source code:
msql=con=cur=''
def connection():
con=msql.connect(host='localhost',user='root',
passwd='student')
if con.is_connected():
cur=con.cursor()
cur.execute('use comp_sci')
Abhishek R
else: 2
print("no connection")
def crt_tables():
connection()
try:
3
except:
try:
cur.execute('use comp_sci')
Description varchar(17),\
PRICE int(5),\
FCODE varchar(3),\
READYDATE date)'
TYPE varchar(15))'
cur.execute(gmt_qry)
cur.execute(fab_qry)
except:
def inst_garment():
4
global msql, con,cur
try:
while True:
cur.execute(qry.format(gcd,dscpn,pc,fcd,rdt))
con.commit()
if ch.lower()!='y':
break
except:
def view_grmnt():
5
global msql, con,cur
rs=cur.fetchall()
print(70*'=')
print('{:<15}{:<15}{:<15}{:<15}{:<15}'
.format('GCode','Description','Price','FCode','ReadyDate')
print(70*'=')
print('%-15s'%row[col],end='')
print()
print(70*'=')
print("Number of Records=",cur.rowcount)
def inst_fabric():
6
print("Insert Records in Table Fabric..")
try:
while True:
tp=input("Enter Type:" )
cur.execute(qry.format(gcd,tp))
con.commit()
if ch.lower()!='y':
break
except:
def view_fabric():
rs=cur.fetchall()
print(' '*5,20*'=')
7
print('%12s%12s'%('FCODE','TYPE'))
print(' '*5,20*'=')
print('{:<12}'.format(row[col]),end='')
print()
print(' '*5,20*'=')
def modify_grmnt():
cur.execute(qry.format(gcd))
rs=cur.fetchall()
if rs:
cur.execute(qry.format(gcd))
8
con.commit()
print("\nSuccessfully updated...")
else:
def del_grmnt():
cur.execute(qry.format(gcd))
rs=cur.fetchall()
is rs:
cur.execute(qry.format(gcd))
con.commit()
else:
connection()
msg="\n\
9
1. Create Tables\n\
3. Insert in Garment\n\
4. Insert in Fabric\n\
7. Exit"
while True:
print(msg)
if ch==1:
crt_tables()
elifch==3:
inst_garment()
elifch==4:
inst_fabric()
elifch==2:
view_grmnt()
view_fabric()
10
elifch==5:
modify_grmnt()
elifch==6:
def_grmnt()
elifch==7:
break
else:
print("invalid Choice...")
con.close()
Output:
1.Create Tables
3. Insert in Garment
4. Insert in Fabric
7. Exit
11
=========================================================================
=========================================================================
=========================================================================
Number of records = 13
12
==========================
FCODE TYPE
==========================
F01Terelene
F02 Cotton
F03 Silk
F04Polyster
F05 Jute
F06 Net
===========================
Number of records= 6
Successfully Updated…
Successfully Deleted…
13
Exp NO: 2 Date:
Aim
Write a program in python for Sql connectivity Consider tables emp and dept tables:
(a) Add a column HRA with default value 2000
(b) Modify HRA for the employee whose empno is input by the user.
(c) Display name, job and annual salary of employees whose comm is assigned in descending
order of salary.
(d) Delete the record of employee whose employee number input by the user
(e) Display empno, name, job and department name who are either a manger or a clerk
(f) Display minimum and maximum, sum and average of salary along with number of
records in each job.
Source Code
import mysql.connector
clm_bool = False
try:
mydb = mysql.connector.connect(host="localhost",user =
'root',password="student",database = 'abhith')
mycurs = mydb.cursor()
except:
print("Connection error")
14
def del_rec():
if mydb.is_connected() :
try:
mydb.commit()
except:
else:
def disp_agg():
if mydb.is_connected() :
15
mycurs.execute('select job,count(*),min(sal),max(sal),sum(sal),avg(sal) from emp group
by job')
rows = mycurs.fetchall()
if rows:
print(tabulate(rows,headers =
('job','count(*)','min(sal)','max(sal)','sum(sal)','avg(sal)'),tablefmt="grid"))
else:
else:
def disp_comm():
if mydb.is_connected() :
mycurs.execute('select empname, job,sal from emp where comm is not NUll order by
saldesc')
rows = mycurs.fetchall()
if rows:
print(tabulate(rows,headers = ('empname','job','sal'),tablefmt="grid"))
else:
16
else:
def disp_mc():
if mydb.is_connected() :
rows = mycurs.fetchall()
if rows:
print(tabulate(rows,headers = ('empno','empname','job','dname'),tablefmt="grid"))
else:
else:
def add_clm():
global clm_bool
17
clm_bool = True
if mydb.is_connected():
try:
except:
else:
def mod_rec():
if mydb.is_connected():
mydb.commit()
else:
18
def disp_table():
if mydb.is_connected() :
rows = mycurs.fetchall()
print(tabulate(rows,headers =
('empno','empname','job','mgr','hiredate','sal','comm','deptno','hra'),tablefmt="grid"))
print(tabulate(rows,headers =
('empno','empname','job','mgr','hiredate','sal','comm','deptno'),tablefmt="grid"))
else:
else:
19
#crt_table()
print('++++++++++++++++++++++++++++++++++++++++++++++')
while True:
choice = int(input('\n\
2) Display name, job and annual salary of employees whose comm is assigned in
descending order of salary\n\
3) Display minimum and maximum, sum and average of salary along with number of
records in each job.\n\
4) Display empno, name, job and department name who are either a manger or a
clerk.\n\
6) Delete a record\n\
7) View Table\n\
8) Exit:'))
if choice==1:
add_clm()
if choice==2:
20
disp_comm()
if choice==3:
disp_agg()
if choice==4:
disp_mc()
if choice==5:
mod_rec()
if choice==6:
del_rec()
if choice==7:
disp_table()
if choice==8:
mydb.close()
break
21
Output
22
23
24
25
Exp NO:3 Date:
Aim: Write a python menu driven program to implement a stack[LIFO] using a list data
structure.
Souce Code:
s=[]
c="y"
while(c=="y"):
print("1. PUSH")
print("2. POP")
print("3. DISPLAY")
if choice==1:
s.append(a)
elif choice==2:
if (s==[]):
print("Stack Empty")
else:
elif choice==3:
26
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
else:
print("Wrong Input")
Ouput:
1. PUSH
2. POP
3. DISPLAY
Enter a number: 5
1. PUSH
2. POP
3. DISPLAY
Enter a number: t
27
1. PUSH
2. POP
3. DISPLAY
Enter a number: 66
1. PUSH
2. POP
3. DISPLAY
66
1. PUSH
2. POP
3. DISPLAY
28
DO you want to Continue?y
1. PUSH
2. POP
3. DISPLAY
29
MYSQL
Exp.NO:4Date:
a) Display min, max, sum and average price for each fabric codes in table Garments.
30
d) Display Fabric Code, description, price and all types of Garments
31
h) Display number of different fabric codes.
i) Display the name of fabrics whose length contains less than 7 characters.
j) Display the name of fabrics ready in 2008( using function year and LIKE -2commands)
32
k) Display Equi-join.
33
Exp. NO:5Date:
Aim:Create a student table with student id, name and marks in 5 subjects
(Eng, Phy, Che, Maths, CS)
34
c) Delete the details of a student in the above table whose mark is not assigned.
f) Order the(Student ID, Name and marks) table in descending order of the marks.
35
g) Add the record-‘s104’,’Catherine’,95,76,NULL,74,63.
36
Exp No:6 Date:
a) Display number of employees, sum and average sal in each department from
tables Emp and Dept.
37
b) Display, Empname, Deptno and dname from tables Emp and Dept.
d) Display number of employees, sum and average sal in each job where number of
employees less than 2.
38
e) Display name of senior most Employee.
39
Consider Tables : Flights and Fares.
40
3) Display Fl_No, Airlines and Fare to be paid for the flights from Delhi to
Mumbai from tables flights and Fares where Fare to be paid=Fare+ Fare*
Tax/100
41
1) SELECT A.Fl_No, No_Flights FROM Flights A, Fares B
WHERE Starting=’Delhi’ AND A.Fl_No=B.Fl_No;
42
Consider Table: Furniture
3. Item name and type in which date of stock is before 22-01-2002 in decreasing
order of item name.
43
4. Item name and date of stock of those items in which discount percentage is more
than 25.
44
Write output of the following:
45
10. SELECT SUM(Price) FROM Furniture WHERE DateofStock<’2002-02-12’;
46