0% found this document useful (0 votes)
85 views18 pages

12 CS Practical File Term-2 2023-24

Here is the Python script to delete records from the category table where name is 'Stockable': ```python import mysql.connector # Connect to database dbcon = mysql.connector.connect( host="localhost", user="root", password="dps", database="items" ) # Get a cursor object cursor = dbcon.cursor() # Delete records where name is 'Stockable' delete_query = "DELETE FROM category WHERE name = 'Stockable'" cursor.execute(delete_query) # Commit changes dbcon.commit() # Close connection dbcon.close() ``` This script connects to

Uploaded by

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

12 CS Practical File Term-2 2023-24

Here is the Python script to delete records from the category table where name is 'Stockable': ```python import mysql.connector # Connect to database dbcon = mysql.connector.connect( host="localhost", user="root", password="dps", database="items" ) # Get a cursor object cursor = dbcon.cursor() # Delete records where name is 'Stockable' delete_query = "DELETE FROM category WHERE name = 'Stockable'" cursor.execute(delete_query) # Commit changes dbcon.commit() # Close connection dbcon.close() ``` This script connects to

Uploaded by

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

21. Write a menu-driven python program to implement stack operation.

def check_stack_isEmpty(stk): if stk==[]:


return True else:
return False
# An empty list to store stack elements, initially empty s=[]
top = None # This is top pointer for push and pop

def main_menu(): while True:


print("Stack Implementation") print("1 - Push")
print("2 - Pop")
print("3 - Peek") print("4 - Display") print("5 - Exit")
ch = int(input("Enter the your choice:")) if ch==1:
el = int(input("Enter the value to push an element:")) push(s,el)
elif ch==2: e=pop_stack(s)
if e=="UnderFlow": print("Stack is underflow!")
else:
print("Element popped:",e) elif ch==3:
e=pop_stack(s)
if e=="UnderFlow":
print("Stack is underflow!")
else:
print("The element on top is:",e) elif ch==4:
display(s) elif ch==5:
break else:
print("Sorry,invalid option")

def push(stk,e): stk.append(e) top = len(stk)-1


def display(stk):
if check_stack_isEmpty(stk): print("Stack is Empty")
else:
top = len(stk)-1 print(stk[top],"-Top")
for i in range(top-1,-1,-1): print(stk[i])
def pop_stack(stk):
if check_stack_isEmpty(stk): return "UnderFlow"
else:
e = stk.pop() if len(stk)==0:
top = None else:
top = len(stk)-1
return e
def peek(stk):
if check_stack_isEmpty(stk): return "UnderFlow"
else:
top = len(stk)-1 return stk[top]

22. Write a program to implement a stack for the employee details (empno, name).
stk=[] top=-1 def line():
print('~'*100)

def isEmpty():
global stk if stk==[]:
print("Stack is empty!!!") else:
None def push():
global stk global top
empno=int(input("Enter the employee number to push:"))

ename=input("Enter the employee name to push:") stk.append([empno,ename])


top=len(stk)-1 def display():
global stk global top if top==-1: isEmpty()
else:
top=len(stk)-1 print(stk[top],"<-top")
for i in range(top-1,-1,-1): print(stk[i])
def pop_ele(): global stk global top
if top==-1: isEmpty() else: stk.pop() top=top-1 def main(): while True:
line()
print("1. Push")
print("2. Pop") print("3. Display") print("4. Exit")

ch=int(input("Enter your choice:")) if ch==1:


push()
print("Element Pushed") elif ch==2:
pop_ele() elif ch==3:
display() elif ch==4:
break
else:
print("Invalid Choice")
23. Write a python program to check whether a string is a palindrome or not using
stack.

stack = [] top = -1
# push function def push(ele):
global top top += 1
stack[top] = ele # pop function
def pop():
global top
ele = stack[top] top -= 1
return ele
# Function that returns 1 if string is a palindrome def isPalindrome(string):
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.

global stack
length = len(string)
# Allocating the memory for the stack stack = ['0'] * (length + 1)
# Finding the mid mid = length // 2 i = 0
while i < mid:
push(string[i]) i += 1
# Checking if the length of the string is odd then neglect the middle character if
length % 2 != 0:
i += 1
# While not the end of the string while i < length:
ele = pop()
# If the characters differ then the given string is not a palindrome if ele !=
string[i]:
return False
i += 1
return True
string = input("Enter string to check:") if isPalindrome(string):
print("Yes, the string is a palindrome")
else:
print("No, the string is not a palindrome")

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

Movie_ID
MovieName
Type
ReleaseDate
ProductionCost
BusinessCost

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 Mithu
Biography
2022/02/04
1000000
800000

M006
Gehraiyaan
Romance
2022/02/11
150000
120000

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;

a.
Display the total charges of patient admitted in the month of November.

b.
Display the eldest patient with name and age.

c.
Count the unique departments.

d.
Display an average charges.

25. Consider the given table patient and Write following queries:

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;

26. Suppose your school management has decided to conduct cricket matches between
students of Class XI and Class XII. Students of each class are asked to join any
one of the four teams � Team Titan, Team Rockers, Team Magnet and Team Hurricane.
During summer vacations, various matches will be conducted between these teams.
Help your sports teacher to do the following:
a)
Create a database �Sports�.

b)
Create a table �TEAM� with following considerations: a.
It should have a column TeamID for storing an integer value between 1 to 9, which
refers to unique identification of a team.

b.
Each TeamID should have its associated name (TeamName), which should be a string
of length not less than 10 characters.

c.
Using table level constraint, make TeamID as the primary key.
c)
Show the structure of the table TEAM using a SQL statement.

d)
As per the preferences of the students four teams were formed as given below.
Insert these four rows in TEAM table: a.
Row 1: (1, Tehlka)
create database and use database command in my sql

b.
Row 2: (2, Toofan)

c.
Row 3: (3, Aandhi)

d.
Row 3: (4, Shailab)

e)
Show the contents of the table TEAM using a DML statement.

f)
Now create another table MATCH_DETAILS and insert data as shown below. Choose
appropriate data types and constraints for each attribute.

MatchID
MatchDate
FirstTeamID
SecondTeamID
FirstTeamScore
SecondTeamScore

M1
2021/12/20
1
2
107
93

M2
2021/12/21
3
4
156
158

M3
2021/12/22
1
3
86
81

M4
2021/12/23
2
4
65
67

M5
2021/12/24
1
4
52
88

M6
2021/12/25
2
3
97
68

Answers:

a)
create database sports;

b)
Creating table with the given specification create table team

(teamid int(1),
teamname varchar(10), primary key(teamid));

c)
desc team;
desc command in mysql
mysql insert command
select command in mysql
create table with a foreign key in mysql

a.
Display the matchid, teamid, teamscore whoscored more than 70 in first ining along
with team name.

b.
Display matchid, teamname and secondteamscore between 100 to 160.

c.
Display matchid, teamnames along with matchdates.

d.
Display unique team names

e.
Display matchid and matchdate played by Anadhi and Shailab.

� Inserting data:
mqsql> insert into team values(1,'Tehlka');
� Show the content of table - team:
select * from team;

� Creating another table:

create table match_details


-> (matchid varchar(2) primary key,
-> matchdate date,
-> firstteamid int(1) references team(teamid),
-> secondteamid int(1) references team(teamid),
-> firstteamscore int(3),
-> secondteamscore int(3));
27. Write following queries:
Answers:

join in mysql

a)
select match_details.matchid, match_details.firstteamid,
team.teamname,match_details.firstteamscore from match_details, team where
match_details.firstteamid = team.teamid and match_details.first

b)
select match_details.matchid, match_details.firstteamid,
team.teamname,match_details.firstteamscore from match_details, team where
match_details.firstteamid = team.teamid and match_details.firstteamscore>70;
join 3
distinct keyword in mysql

c)
select matchid, teamname, firstteamid, secondteamid, matchdate from match_details,
team where match_details.firstteamid = team.teamid;

d)
select distinct(teamname) from match_details, team where match_details.firstteamid
= team.teamid;

e)
select matchid,matchdate from match_details, team where match_details.firstteamid
= team.teamid and team.teamname in ('Aandhi','Shailab');
order by in my sql

a.
Display all the items in the ascending order of stockdate.

b.
Display maximum price of items for each dealer individually as per dcode from
stock.

c.
Display all the items in descending orders of itemnames.

d.
Display average price of items for each dealer individually as per doce from stock
which avergae price is more than 5.

e.
Diisplay the sum of quantity for each dcode.

28. Consider the following table and write the queries:

itemno
Item
dcode
qty
unitprice
stockdate

S005
Ballpen
102
100
10
2018/04/22
S003
Gel Pen
101
150
15
2018/03/18

S002
Pencil
102
125
5
2018/02/25

S006
Eraser
101
200
3
2018/01/12

S001
Sharpner
103
210
5
2018/06/11

S004
Compass
102
60
35
2018/05/10

S009
A4 Papers
102
160
5
2018/07/17

Answers:
a)
select * from stock order by stockdate;
use of order by desc in mysql

b)
select dcode,max(unitprice) from stock group by code;

c)
select * from stock order by item desc;

d)
select dcode,avg(unitprice) from stock group by dcode having avg(unitprice)>5;
e)
select dcode,sum(qty) from stock group by dcode;

29. Write a Python database connectivity script that deletes records from category
table of database items that have name =�Stockable� .
import mysql.connector as ctor
dbcon = ctor.connect (host=�localhost�, user= �root�, passwd=�dps�,
database=�items�)
cursor=dbcon.cursor()
s=�Delete from category where name =�%S� �
d=(�Stockable�,)
cursor.execute(s,d)
dbcon.commit()
print(�Rows affected : �,cursor.rowcount)
dbcon.close()
30. Write a Python database connectivity script that implement where clause to
view all records: name, age, marks from student where city is Delhi.
import mysql.connector as sqlc
myc=sqlc.connect(host= �localhost�, user= �root�, passwd=�dps�, database=
�school�)
cursor=myc.cursor()
cursor.execute(� Select name, age, marks from student where city= �Delhi� �)
rec= cursor.fetchall()
for x in rec:
print(x)

31. Write a Python database connectivity script that implement update command
where name is Vinay and his age to 28
import mysql.connector as sqlc
myc=sqlc.connect(host= �localhost�, user= �root�, passwd=�dps�, database=
�school�)
cursor=myc.cursor()
cursor.execute(�Update student set age=28 where name = � Vinay� �)
myc.commit()
print(cursor.rowcount, �Record (s) updated�)

32. Write a Python database connectivity script that implement insert command to
insert record of student
import mysql.connector as sqlc
myc=sqlc.connect(host= �localhost�, user= �root�, passwd=�dps�, database=
�school�)
cursor=myc.cursor()
cursor.execute(�Insert into student values (�Ritu�, �Science�, 345, �B�, 12) �)
myc.commit()
print(cursor.rowcount, �Record added�)
33. Write a Python database connectivity script that implement insert command to
insert record of student by accepting values from the user.

34.

You might also like