Practical PRG Codes and SQL Queries
Practical PRG Codes and SQL Queries
#SET 1
import pickle,os
def create():
f=open("player.dat","ab")
for i in range(2):
pcode =input("Enter the code")
pname =input("Enter name")
score = int(input("Enter score value"))
rank = int(input("Enter rank"))
new = [pcode,pname,score,rank]
pickle.dump(new,f)
f.close()
def display():
f=open("player.dat","rb")
try:
while True:
x=pickle.load(f)
print(x)
except:
f.close()
def search():
f=open("player.dat","rb")
s = input("Enter the player code to be searched")
try:
while True:
x=pickle.load(f)
if x[0] == s:
print(x[0],x[1],x[2],x[3])
except:
f.close()
while True:
print("1.Create")
print("2. display")
print("3. Search")
print("4. Exit")
ch = int(input("Enter choice"))
if ch ==1:
create()
elif ch == 2:
display()
elif ch == 3:
search()
elif ch == 4:
break
else:
break
c) To display the Consumer Name, City, Company and the price details.
Select Consumername, city, company, price from stationery, Consumer where
stationaery.s_id=consumer.s_id;
-------------------------------------------------------------------------------------
#SET 2
#Min and max
def maxvalue(a):
maxi = a[0]
for i in a:
if i > maxi:
maxi = i
print("Maximum value=",maxi)
def minvalue(a):
mini = a[0]
for i in a:
if i < mini:
mini = i
print("Minimum value=",mini)
a = eval(input("Enter list"))
print(a)
maxvalue(a)
minvalue(a)
#set 2 text file
def createtext():
f = open("exam.txt","w")
f.write("123abc exam ExAM CBSE @!*")
f.write("This is 2 batch in Python 3.10")
f.close()
def readtext():
f = open("exam.txt","r")
x = f.read()
cu=cl=cv=cd=cs=0
for i in x:
if i.isupper():
cu = cu +1
elif i.islower():
cl = cl +1
elif i.isdigit():
cd = cd +1
else:
cs= cs +1
if i in "aeiouAEIOU":
cv = cv + 1
print(cu,cv,cl,cs,cd)
f.close()
createtext()
readtext()
a) To display the details of all the items in ascending order of item names.
Select * from item order by Iname;
b) To display item name and price of all those items, whose price is in the range of
10000 and 22000
Select Iname, price from item where price between 10000 and 22000;
c) Delete the details of the DIGITAL CAMERA from the table.
Delete from item where Iname=’ DIGITAL CAMERA’;
d) To display the item name, price, trader name and quantity of those item which
have
quantity more than 150.
Select Iname, price, Tname, qty from Item, Trader where Item.Tcode=Trader.Tcode
and qty >150.
#SET 3
import csv
def create():
f=open("games.csv","w",newline ="")
wobj=csv.writer(f)
for i in range(2):
gid=input("Enter Gid:")
gname=input("Enter Gname:")
points=int(input("Enter points"))
x=[gid,gname,points]
wobj.writerow(x)
print("File created")
f.close()
def searchcsv():
f = open("games.csv","r",newline="")
robj = csv.reader(f)
g=input("Enter gameid to search")
found = False
for i in robj:
if i[0]=="gid":
pass
elif i[0] == g:
print("Game id",i[0])
print("Game name",i[1])
print("Points",i[2])
found = True
if found == False:
print("Data not found")
def display():
f=open("games.csv","r",newline='')
robj=csv.reader(f)
for i in robj:
print("Gid",i[0])
print("Gname",i[1])
print("Points",i[2])
f.close()
while True:
print("1. Create")
print("2. Display")
print("3. Search")
print("4. Exit")
ch =int(input("Enter choice:"))
if ch==1:
create()
elif ch == 2:
display()
elif ch == 3:
searchcsv()
elif ch== 4:
break
else:
break
print("Data not found")
a. Display Name and department of all doctors who are in “MEDICINE” having more than 10
years of experience.
Select name, dept from doctor where dept=“MEDICINE” and EXPERIENCE > 10;
-------------------------------------------------------------------------------------
#SET 4
import mysql.connector as ms
con = ms.connect(host = "localhost", user = "root", password = "tiger", database = "employee")
cur = con.cursor()
def createtable():
query = "create table empl(eno int primary key, ename varchar(20), esal float)"
cur.execute(query)
print("Table Created")
def insertrecords():
n = int(input("Enter number of records: "))
for i in range(n):
eno = int(input("Enter employee number: "))
ename = input("Enter employee name: ")
esal = float(input("Enter employee salary: "))
query = "insert into empl values({}, '{}', {})".format(eno, ename, esal)
cur.execute(query)
con.commit()
def displayrecords():
query = "select * from empl"
cur.execute(query)
data = cur.fetchall()
for i in data:
print( i)
con.close()
while True:
print(" Press 1 to create a table empl")
print("Press 2 to input records for table empl")
print("Press 3 to display records")
ch = int(input("Enter choice: "))
if ch == 1:
createtable()
elif ch ==2:
insertrecords()
elif ch==3:
displayrecords()
else:
break
b) Increase the salary of all the employees by 10% who lives in Mumbai or Delhi.
Update EMPLOYEE set salary=salary+salary*0.1 where
DEPT.DCODE=EMPLOYEE.DCODE and LOCATION in
(’DELHI’,’MUMBAI’);
c) To display WNO, NAME of those workers from the table EMPLOYEE who are
born
between ‘1987-01-01’ and ‘1991-12-01’.
Select WNO, NAME from EMPLOYEE where DOB between ‘1987-01-
01’ and ‘1991-12-01’;
#SET 5
#Linear search
def linearsearch(a):
x = int(input("Enter the element to be searched"))
found = False
for i in range(len(a)):
if a[i] == x:
pos = i +1
print("Position=",pos)
found = True
if found == False:
print("Element not found")
a= eval(input("ENter the list of integers"))
linearsearch(a)
#SET 5 AM count
def creation():
f = open("article.txt","w")
f.write("Money is precious\n")
f.write("Me My a an the an Mother\n")
f.write("After a long time\n")
f.close()
def AMCount():
f = open("article.txt","r")
countm = counta = 0
x = f.read()
for i in x:
if i.lower() == "m":
countm = countm+1
elif i.lower() =="a":
counta = counta+1
print('Count M =',countm)
print("countA =",counta)
f.close()
creation()
AMCount()
1. To display department code, sum of salary for each department where the count of
dcode is greater than 1.
Select DCODE, sum(salary) from EMPLOYEE group by DCODE having
count(DCODE) >1;
#SET 6
stk =[]
def Push(emp):
for i in emp:
x = emp[i]
if x > 25000:
stk.append(i)
print("STack is created",stk)
def POP():
while len(stk) !=0:
y = stk.pop()
print(y)
print("stack empty")
emp = eval(input("Enter emp details as dictionary"))
Push(emp)
POP()
c) To display the Consumer Name, City, Company and the price details.
Select Consumername, city, company, price from stationery, Consumer where
stationaery.s_id=consumer.s_id;
d) To decrease the Price of all Stationery items by 2.
Update stationery set price = price-2;
-------------------------------------------------------------------------------------
import mysql.connector as ms
con = ms.connect(host = "localhost", user = "root", password = "tiger", database = "employee")
cur = con.cursor()
def createtable():
query = "create table empl(eno int primary key, ename varchar(20), esal float)"
cur.execute(query)
def insertrecords():
n = int(input("Enter number of records: "))
for i in range(n):
eno = int(input("Enter employee number: "))
ename = input("Enter employee name: ")
esal = float(input("Enter employee salary: "))
query = "insert into empl values({}, '{}', {})".format(eno, ename, esal)
cur.execute(query)
con.commit()
def updaterecords():
query = "update empl set esal = esal + 10/100* esal"
query1 = "select esal from empl"
cur.execute(query)
con.commit()
cur.execute(query1)
data = cur.fetchall()
for i in data:
print("Updated salary: ", i)
con.close()
while True:
print(" Press 1 to create a table empl")
print("Press 2 to input records for table empl")
print("Press 3 to update employee salary and see the updated records")
ch = int(input("Enter choice: "))
if ch == 1:
createtable()
elif ch ==2:
insertrecords()
elif ch==3:
updaterecords()
else:
break
a) To display the details of all the items in descending order of item names.
Select * from item order by Iname desc;
b) To display item name and price of all those items, whose price is in the range of
10000 and 22000
Select Iname, price from item where price between 10000 and 22000;
c) To display the item name, price, trader name and quantity of those items which
have
quantity less than150.
Select Iname, price, Tname, qty from Item, Trader where
Item.Tcode=Trader.Tcode and qty < 150.
-------------------------------------------------------------------------------------
# Set 8
import pickle,os
def create():
f=open("player.dat","ab")
for i in range(2):
pcode =input("Enter the code")
pname =input("Enter name")
score = int(input("Enter score value"))
rank = int(input("Enter rank"))
new = [pcode,pname,score,rank]
pickle.dump(new,f)
f.close()
def delete():
f = open("player.dat","rb")
g=open("temp.dat","wb")
found = False
eno = input("Enter the player eno")
try:
while True:
new = pickle.load(f)
if new[0] != eno:
pickle.dump(new,g)
found = True
except:
if found == False:
print("Record not found")
f.close()
g.close()
os.remove("player.dat")
os.rename("temp.dat","player.dat")
def display():
f=open("player.dat","rb")
try:
while True:
x=pickle.load(f)
print(x)
except:
f.close()
while True:
print("1.Create")
print("2.delete")
print("3. display")
print("5. Exit")
ch = int(input("Enter choice"))
if ch ==1:
create()
elif ch == 2:
delete()
elif ch == 3:
display()
elif ch ==4:
break
a) Display Name and department of all doctors who are in “MEDICINE” and whose name
starts with the letter “B”
Select name, dept from doctor where dept=“MEDICINE” and NAME like ‘B%’;
def update():
f=open("games.csv","r",newline ='')
g=open("new.csv","w",newline ='')
robj=csv.reader(f)
wobj=csv.writer(g)
id=input("Enter Gid to be updated:")
for i in robj:
if i[0] == id:
points=int(input("ENter updated points:"))
x =[i[0],i[1],points]
wobj.writerow(x)
else:
wobj.writerow(i)
f.close()
g.close()
def display():
f=open("new.csv","r",newline='')
robj=csv.reader(f)
for i in robj:
print("Gid",i[0])
print("Gname",i[1])
print("Points",i[2])
f.close()
while True:
print("1. Create")
print("2. update")
print("3. display")
print("4. Exit")
ch =int(input("Enter choice:"))
if ch==1:
create()
elif ch == 2:
update()
elif ch == 3:
display()
elif ch== 4:
break
c) To display WNO, NAME of those workers from the table EMPLOYEE who are born
between ‘1987-01-01’ and ‘1991-12-01’.
Select WNO, NAME from EMPLOYEE where DOB between
‘1987-01-01’ and ‘1991-12-01’;
d) To display the count of female employees who have joined after ‘1986-01-01’
Select count(gender) from EMPLOYEE group by GENDER having DOJ
>
‘1986-01-01’ and GENDER=’FEMALE’;
-------------------------------------------------------------------------------------
#SET 10
xiia=[]
def PushElement(a):
if a[1].lower()=="xii" and a[2].lower()=="a":
newlist = [a[0],a[3]]
xiia.append(newlist)
print("newly created stack",xiia)
def PopElement():
while(len(xiia))!=0:
y = xiia.pop()
print(y)
while True:
print("1.Push")
print("2.Pop")
print("3.Exit")
ch =int(input("Enter choice"))
if ch ==1:
a= eval(input("ENter the list"))
PushElement(a)
elif ch == 2:
PopElement()
elif ch == 3:
break
a) To display total number of employees and department code in each department.
Select count (WNO), DCODE from EMPLOYEE group by DCODE;
b) To display department code, sum of salary for each department having number of
employees greater than 1
Select DCODE, sum(salary) from EMPLOYEE group by DCODE
having count(DCODE) > 1;