0% found this document useful (0 votes)
32 views24 pages

CS Practical File 2022-23

Uploaded by

mm.mansimishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views24 pages

CS Practical File 2022-23

Uploaded by

mm.mansimishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ACKNOWLEDGEMENT

I wish to express my deep sense of gratitude and indebtedness to Rajesh Kumar Pandey, PGT
Computer Science, Ascent International School, Greater Noida for his invaluable help, advice
and guidance in the preparation of practical report.

I am also greatly indebted to our Vice Principal Ms. Mahima Bhatt and Principal Mr. Birendra
Bansal for providing me with the facilities and requisite laboratory conditions for making this
practical file.

Samriddhi Shree
ASCENT INTERNATIONAL SCHOOL
Affiliated to CBSE, Delhi, Affiliation No. 2130772, School Code 60349

DEPARTMENT OF COMPUTER SCIENCE


CERTIFICATE

This is to certify that Samriddhi Shree, student of Class XII, Ascent International School has
completed the PRACTICAL FILE during the academic year 2022-23 towards partial fulfillment of
credit for the Computer Science practical evaluation of CBSE SSCE-2023 and submitted
satisfactory report, as compiled in the following pages, under my supervision.

Internal Examiner External Examiner

Date: Date:
SQL QUERIES

1. Consider the following MOVIE table and write the SQL queries based on it.

MovieName Type ReleaseDate ProductionCost BusinessCost


Movie_ID
M001 Dahek Action 2022/01/26 1245000 1300000
M002 Attack Action 2022/01/28 1120000 1250000
M003 Looop Lapeta Thriller 2022/02/01 250000 300000
M004 Badhai Do Drama 2022/02/04 720000 68000
M005 Shabaash Biography 2022/02/04 1000000 800000
M006 Gehraiyaan Romance 2022/02/11 150000 120000

a) Display all information from movie.


b) Display the type of movies.
c) Display movieid, moviename, total_eraning by showing the business done by the movies.
Claculate the business done by movie using the sum of productioncost and businesscost.
d) Display movieid, moviename and productioncost for all movies with productioncost
greater thatn 150000 and less than 1000000.
e) Display the movie of type action and romance.
f) Display the list of movies which are going to release in February, 2022.

Answers:
a) select * from movie;
b) select distinct from a movie;

c) select movieid, moviename, productioncost + businesscost "total earning" from movie;

d) select movie_id,moviename, productioncost from movie where producst is >150000 and


<1000000;

e) select moviename from movie where type ='action' or type='romance';


f) select moviename from movie where month(releasedate)=2;

g) Consider the given table patient and Write following queries:

(i) Display the total charges of patient admitted in the month of November.
(ii) Display the eldest patient with name and age.
(iii) Count the unique departments.
(iv) Display an average charges.
Answers:
a) select sum(charges) from patient where dateofadm like '%-11-%';

b) select pname,max(age) from patient;


c) Select count(distinct department) from patient;

d) Select avg(charges) from patient;

1. Perform all the operations with reference to table ‘students’ through MySQL-Python
connectivity.
Answers:
1. Using pymysql - Code:
import pymysql as ms

#Function to create Database as per users choice

def c_database():

try:

dn=input("Enter Database Name=")

c.execute("create database {}".format(dn))

c.execute("use {}".format(dn))

print("Database created successfully")

except Exception as a:

print("Database Error",a)

#Function to Drop Database as per users choice

def d_database():

try:

dn=input("Enter Database Name to be dropped=")

c.execute("drop database {}".format(dn))


print("Database deleted sucessfully")

except Exception as a:

print("Database Drop Error",a)

#Function to create Table


def c_table():
try:
c.execute('''create table students
(
rollno int(3),
stname varchar(20)
);
''')
print("Table created successfully")
except Exception as a:
print("Create Table Error",a)

#Function to Insert Data


def e_data():
try:
while True:
rno=int(input("Enter student rollno="))
name=input("Enter student name=")
c.execute("use {}".format('school'))
c.execute("insert into students values({},'{}');".format(rno,name))
db.commit()
choice=input("Do you want to add more record<y/n>=")
if choice in "Nn":
break
except Exception as a:
print("Insert Record Error",a)

#Function to Display Data


def d_data():
try:
c.execute("select * from students")
data=c.fetchall()
for i in data:
print(i)
except Exception as a:
print("Display Record Error",a)
db=ms.connect(host="localhost",user="root",password="root")
c=db.cursor()
while True:
print("MENU\n1. Create Database\n2. Drop Database \n3. Create Table\n4. Insert Record \n5.
Display Entire Data\n6. Exit")
choice=int(input("Enter your choice<1-6>="))
if choice==1:
c_database()
elif choice==2:
d_database()
elif choice==3:
c_table()
elif choice==4:
e_data()
elif choice==5:
d_data()
elif choice==6:
break
else:
print("Wrong option selected")
Output:
2. using mysqlconnector
import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",database='school')
cn=db.cursor()
def insert_rec():
try:
while True:
rn=int(input("Enter roll number:"))
sname=input("Enter name:")
marks=float(input("Enter marks:"))
gr=input("Enter grade:")
cn.execute("insert into students values({},'{}',{},'{}')".format(rn,sname,marks,gr))
db.commit()
ch=input("Want more records? Press (N/n) to stop entry:")
if ch in 'Nn':
break
except Exception as e:
print("Error", e)

def update_rec():
try:
rn=int(input("Enter rollno to update:"))
marks=float(input("Enter new marks:"))
gr=input("Enter Grade:")
cn.execute("update students set marks={},grade='{}' where rno={}".format(marks,gr,rn))
db.commit()
except Exception as e:
print("Error",e)

def delete_rec():
try:
rn=int(input("Enter rollno to delete:"))
cn.execute("delete from students where rno={}".format(rn))
db.commit()
except Exception as e:
print("Error",e)
def view_rec():
try:
cn.execute("select * from students")

import mysql.connector as ms

db=ms.connect(host="localhost",user="root",passwd="root",database='school')

cn=db.cursor()

def insert_rec():

try:

while True:

rn=int(input("Enter roll number:"))

sname=input("Enter name:")

marks=float(input("Enter marks:"))

gr=input("Enter grade:")

cn.execute("insert into students values({},'{}',{},'{}')".format(rn,sname,marks,gr))

db.commit()

ch=input("Want more records? Press (N/n) to stop entry:")

if ch in 'Nn':

break

except Exception as e:

print("Error", e)

def update_rec():

try:

rn=int(input("Enter rollno to update:"))

marks=float(input("Enter new marks:"))

gr=input("Enter Grade:")

cn.execute("update students set marks={},grade='{}' where rno={}".format(marks,gr,rn))

db.commit()

except Exception as e:

print("Error",e)
def delete_rec():

try:

rn=int(input("Enter rollno to delete:"))

cn.execute("delete from students where rno={}".format(rn))

db.commit()

except Exception as e:

print("Error",e)

def view_rec():

try:

cn.execute("select * from students")

data=c.fetchall()

for i in data:

print(i)

except Exception as e:

print("Error",e)

while True:

print("MENU\n1. Insert Record\n2. Update Record \n3. Delete Record\n4. Display Record \n5.

Exit")

ch=int(input("Enter your choice<1-4>="))

if ch==1:

insert_rec()

elif ch==2:

update_rec()

elif ch==3:

delete_rec()

elif ch==4:

view_rec()

elif ch==5:
break

else:

print("Wrong option selected")

except Exception as e:

print("Error",e)

while True:

print("MENU\n1. Insert Record\n2. Update Record \n3. Delete Record\n4. Display Record \n5.

Exit")

ch=int(input("Enter your choice<1-4>="))

if ch==1:

insert_rec()

elif ch==2:
update_rec()
elif ch==3:
delete_rec()
elif ch==4:
view_rec()
elif ch==5:
break
else:
print("Wrong option selected")
Output:
3. Write a menu-driven program to store data into a MySQL database named shop and table
customer as following:

1. Add customer details


2. Update customer details
3. Delete customer details
4. Display all customer details

import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",database='mydb')
cn=db.cursor()
def insert_rec():
try:
while True:
cid=int(input("Enter customer id:"))
cname=input("Enter name:")
city=input("Enter city:")
bill_amt=float(input("Enter bill amount:"))
cat=input("Enter category:")
cn.execute("insert into customer
values({},'{}','{}',{},'{}')".format(cid,cname,city,bill_amt,cat))
db.commit()
ch=input("Want more records? Press (N/n) to stop entry:")
if ch in 'Nn':
break
except Exception as e:
print("Error", e)

def update_rec():
try:
cn.execute("select * from customer")
data=cn.fetchall()
for i in data:
ci=i[0]
cna=i[1]
ct=i[2]
b=i[3]
c=i[4]
cid=int(input("Enter customer id to update:"))
if cid==ci:
ch_cname=input("Want to update Name, Press 'Y':")
if ch_cname.lower()=='y':
cname=input("Enter new name:")
else:
cname=cna
ch_city=input("Want to update city, Press 'Y':")
if ch_city.lower()=='y':
city=input("Enter new city:")
else:
city=ct
ch=input("Want to update bill amount, Press 'Y':")
if ch.lower()=='y':
bill_amt=float(input("Enter new bill amount:"))
else:
bill_amt=b
ch_cat=input("Want to update Category, Press 'Y':")
if ch_cat.lower()=='y':
cat=input("Enter new category:")
else:
cat=c
cn.execute("update customer set cname='{}', city='{}', bill_amt={},category='{}' where
cust_id={}".format(cname,city,bill_amt,cat,cid))
db.commit()
else:
print("Record Not Found...")
except Exception as e:
print("Error",e)
def delete_rec():
try:
cid=int(input("Enter rollno to delete:"))
cn.execute("delete from customer where cust_id={}".format(cid))
db.commit()
except Exception as e:
print("Error",e)
def view_rec():
try:
cn.execute("select * from customer")
data=cn.fetchall()
cnt=0
for i in data:
cnt=cnt+1
print("Record:",cnt)
print('~'*50)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
print('~'*50)
except Exception as e:
print("Error",e)
while True:
print("MENU\n1. Insert Record\n2. Update Record \n3. Delete Record\n4. Display Record \n5.
Exit")
ch=int(input("Enter your choice<1-4>="))
if ch==1:
insert_rec()
elif ch==2:
update_rec()
elif ch==3:
delete_rec()
elif ch==4:
view_rec()
elif ch==5:
break
else:
print("Wrong option selected")
Output:
4. Modify the above program and display the customer details based on the following menu:

1. Display customer details by city


2. Display customer details by bill amount
3. Display customer details by name
4. Display customer details by category

import mysql.connector as ms
db=ms.connect(host="localhost",user="root",passwd="root",database='mydb')
cn=db.cursor()

def byCity():
try:
city=input("Enter city to search:")
cn.execute("select * from customer where city='{}'".format(city))
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
print("No records found for city ", city)
except Exception as e:
print("Error",e)

def byBillAmt():
try:
ba=input("Enter the bill amount:")
cn.execute("select * from customer where bill_amt={}".format(ba))
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
print("No records found for bill amount ", ba)
except Exception as e:
print("Error",e)

def byName():
try:
name=input("Enter the name:")
cn.execute("select * from customer where cname='{}'".format(name))
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
print("No records found for ", name)
except Exception as e:
print("Error",e)

def byCat():
try:
cat=input("Enter the cat:")
cn.execute("select * from customer where category='{}'".format(cat))
data=cn.fetchall()
if data!=[]:
cnt=0
for i in data:
cnt=cnt+1
print('~'*100)
print("Record:",cnt)
print('~'*100)
print("Customer ID:",i[0])
print("Customer Name:",i[1])
print("City:",i[2])
print("Bill Amount:",i[3])
print("Category:",i[4])
else:
print("No records found for category ", cat)
except Exception as e:
print("Error",e)

while True:
print('''
MENU
1.Display customer details by city
2.Display customer details by bill amount
3.Display customer details by name
4.Display customer details by category
5.Exit
''')
ch=int(input("Enter your choice<1-4>="))
if ch==1:
byCity()
elif ch==2:
byBillAmt()
elif ch==3:
byName()
elif ch==4:
byCat()
elif ch==5:
break
else:
print("Wrong option selected")
Output:

You might also like