You are on page 1of 55

COMPUTER SCIENCE

PRACTICAL JOURNAL TERM II

Chaitanya Obhan
  XII A, Roll no 9
Index
Sr no Practical Number Page no Signature
1 Practical 1 3-6
2 Practical 2 7 - 10
3 Practical 3 11 - 14
4 Practical 4 15 - 18
5 Practical 5 19 - 22
6 Practical 6 23 - 26
7 Practical 7 27 - 32
8 Practical 8 33 - 36
9 Practical 9 37 - 39
10 Practical 10 40 - 42
11 Practical 11 43 - 45
12 Practical 12 46 - 47
13 Practical 13 48 - 51
14 Practical 14 52
15 Practical 15 53 - 54

1|Page
Practical Number 1

Question:
Consider the following tables Stationery and Consumer. Write the outputs for SQL statements (i) and (ii) and SQL
statements for (iii) to (vii).

Table: Stationery
Structure:
Field Name Data Type Size Constraint
S_id Char 10 Primary Key
Stationeryname Char 20
Company Char 10
Price Integer Not null
Data:
S_id Stationeryname Company Price
DP01 Dot pen ABC 10
PL02 Pencil XYZ 6
ER05 Eraser XYZ 7
PL01 Pencil CAM 5
GP02 Gel pen ABC 15
Table: Consumer
Structure:
Field Name Data Type Size Constraint
C_id Integer Primary Key
Consumername Char 20
Address Char 20
S_id Char 10 Foreign key referencing table
Stationery column S_id
Data:
C_id Consumername Address S_id
01 Good learner Delhi PL01
06 Write well Mumbai GP02
12 Topper Delhi DP01
15 Write & draw Delhi PL02
16 Motivation Bangalore PL01

2|Page
i. Select s_id, company, max(price), min(price) from stationery group by company; ii.
Select stationery.s_id, c_id, address from stationery, consumer where stationery.s_id =
consumer.s_id and stationeryname=’Pencil’;
iii. Display the consumer name, address, company, price for those whose price is not more than 10. iv.
Display the companies whose consumers are located in delhi.
v. Display the stationery name and stationery id of all the items which ends with pen.
vi. Increase the price of sid PL01 by 20.
vii. Add one more column phone in the table consumer with data type char(12).

Solution:
Creating table and inserting values

3|Page
i. Select s_id, company, max(price), min(price) from stationery group by company;

4|Page
ii. Select stationery.s_id, c_id, address from stationery, consumer where stationery.s_id =
consumer.s_id and stationeryname=’Pencil’;

iii. Display the consumer name, address, company, price for those whose price is not more than 10.

iv. Display the companies whose consumers are located in delhi.

v. Display the stationery name and stationery id of all the items which ends with pen.

5|Page
vi. Increase the price of sid PL01 by 20.

vii. Add one more column phone in the table consumer with data type char(12).

6|Page
Practical Number 2
In a database there are two tables Ticketdetails and Agentdetails with the following data. Write the outputs for SQL
statements (i) and (ii) and SQL statements for (iii) to (vii).

Table: Agentdetails
Structure:
Field Name Data Type Size Constraint
Acode Char 10 Primary Key
Aname Char 20
Data:
Acode Aname
A01 Mr robin
A02 Mr alok
A03 Mr trilok
A04 Mr john
Table: Ticketdetails
Structure:
Field Name Data Type Size Constraint
Tcode Char 10 Primary key
Tname Char 20
Tickets Integer
Acode Char 10 Foreign key referencing table
Agentdetails column Acode
Data:
Tcode Tname Tickets Acode
S001 Meena 7 A01
S002 Vani 5 A02
S003 Meena 9 A01
S004 Krish 2 A03
S005 Suraj 1 A02
i. Select tcode, ticketdetails.acode from ticketdetails, agentdetails where ticketdetails.acode =
agentdetails.acode and ticketdetails.acode=’A02’;
ii. Select tcode, tname, sum(tickets) from ticketdetails group by acode;
iii. Display ticket code, ticket name and agent name of all the records where the number of tickets sold
is more than 5.
iv. Display total number of tickets booked by agent ‘Mr alok’.
v. Display agent code, agent name and corresponding ticket code where agent name ends with ‘k’.
vi. Change the data type of tcode column of ticketdetails table to char(20). vii. Remove those ticket
details where number of tickets is less than 5.

7|Page
Solution:
Creating table and inserting values

8|Page
i. Select tcode, ticketdetails.acode from ticketdetails, agentdetails where ticketdetails.acode =
agentdetails.acode and ticketdetails.acode=’A02’;

ii. Select tcode, tname, sum(tickets) from ticketdetails group by acode;

iii. Display ticket code, ticket name and agent name of all the records where the number of tickets
sold is more than 5.

iv. Display total number of tickets booked by agent ‘Mr alok’.

v. Display agent code, agent name and corresponding ticket code where agent name ends with ‘k’.

vi. Change the data type of tcode column of ticketdetails table to char(20).

9|Page
vii. Remove those ticket details where number of tickets is less than 5.

10 | P a g e
Practical Number 3
In a database there are two tables Sams and Vendor with the following data. Write the outputs for SQL statements (i)
and (ii) and SQL statements for (iii) to (vii).

Table: Vendor
Structure:
Field Name Data Type Size Constraint
Vcode Char 10 Primary Key
Vname Char 20
Data:
Vcode Vname
P01 Satish
P02 Manoj
P03 Subodh
P04 Jacob
Table: Sams Structure:
Field Name Data Type Size Constraint
Icode Char 10 Primary key
Iname Char 20
Price Integer Not null
Colour Char 10
Vcode Char 10 Foreign key referencing table
Vendor column Vcode
Data:
Icode Iname Price Colour Vcode
S001 Refrigerator 20000 Blue P01
S002 Mobile phone 45000 Black P02
S003 LCD 60000 Silver P03
S004 Washing machine 12500 Smoke P01
S005 Air conditioner 16000 White P03
i. Select vcode, max(price) from sams group by vcode;
ii. Select Vendor.vcode, Icode, Iname from vendor, sams where vendor.vcode = sams.vcode and colour
in(‘Silver’,’White’);
iii. Display icode, iname and vname of all the vendors who manufacture ‘refrigerator’. iv. Display
iname, icode, vname and price of all the products whose price is more than 20000. v. Display
item names of all items manufactured by vendor whose code is ‘P03’.
11 | P a g e
vi. Decrease the price of all the items by 1000 who are manufactured by vendor ‘P01’.
vii. Remove the column of colour from sams table.

Solution:
Creating table and inserting values

12 | P a g e
i. Select vcode, max(price) from sams group by vcode;

ii. Select Vendor.vcode, Icode, Iname from vendor, sams where vendor.vcode = sams.vcode and colour
in(‘Silver’,’White’);

iii. Display icode, iname and vname of all the vendors who manufacture ‘refrigerator’.

iv. Display iname, icode, vname and price of all the products whose price is more than 20000.

v. Display item names of all items manufactured by vendor whose code is ‘P03’.

13 | P a g e
vi. Decrease the price of all the items by 1000 who are manufactured by vendor ‘P01’.

vii. Remove the column of colour from sams table.

14 | P a g e
Practical Number 4
In a database there are two tables Handset and Customer with the following data. Write the outputs for SQL statements
(i) and (ii) and SQL statements for (iii) to (vii).

Table: Handset
Structure:
Field Name Data Type Size Constraint
Setcode Char 10 Primary key
Setname Char 20
Touchscreen Char 5
Phonecost Integer Not null
Data:
Setcode Setname Touchscreen Phonecost
N1 Nokia 2G N 5000
N2 Nokia 3G Y 8000
N3 Blackberry N 14000
Table: Customer
Structure:
Field Name Data Type Size Constraint
Custno Integer Primary Key
Setno Char 5 Foreign key referencing table
Handset column Setcode
Custaddress Char 20
Data:
Custno Setno Custaddress
1 N2 Delhi
2 N3 Mumbai
3 N2 Mumbai
4 N1 Kolkata
5 N3 Delhi
i. Select setno, setname from handset, customer where handset.setcode = customer.setno
and custaddress=’delhi’;
ii. Select setno, count(*) from customer group by setno; iii. Display the custno,
custaddress and corresponding setname for each customer. iv. Display the
customer details for each customer who uses a nokia handset. v. Change the
customer address for customer number 4 to Delhi.
vi. Remove the column of touchscreen from handset table.
vii. Delete the handsets whose cost is less than 7500.

15 | P a g e
Solution:
Creating table and inserting values

i. Select setno, setname from handset, customer where handset.setcode = customer.setno and
custaddress=’delhi’;

16 | P a g e
ii. Select setno, count(*) from customer group by setno;

iii. Display the custno, custaddress and corresponding setname for each customer.

iv. Display the customer details for each customer who uses a nokia handset.

v. Change the customer address for customer number 4 to Delhi.

17 | P a g e
vi. Remove the column of touchscreen from handset table.

vii. Delete the handsets whose cost is less than 7500.

18 | P a g e
Practical Number 5
In a database there are two tables Job and Employee with the following data. Write the outputs for SQL statements (i)
and (ii) and SQL statements for (iii) to (vii).

Table: Job Structure:


Field Name Data Type Size Constraint
Jobid Integer Primary Key
Jobtitle Char 30
Salary Integer
Data:
Jobid Jobtitle Salary
101 President 200000
102 Vice president 125000
103 Administration assistant 80000
104 Accounting manager 70000
105 Accountant 65000
106 Sales manager 80000
Table: Employee
Structure:
Field Name Data Type Size Constraint
Employeeid Char 5 Primary Key
Name Char 20
Sales Integer
Jobid Integer Foreign key referencing table
Job column Jobid
Data:
Employeeid Name Sales Jobid
E1 Sumit Sinha 1100000 102
E2 Vijay Singh Tomar 1300000 101
E3 Ajay Rajpal 1400000 103
E4 Mohit Ramnani 1250000 102
E5 Shailja Singh 1450000 103
i. Select max(sales) from employee group by jobid;
ii. Select job.jobid, employeeid, name from job, employee where job.jobid = employee.jobid and
sales>1350000;
iii. Display employee id, names of employees, job id with corresponding job titles. iv. Display the
minimum sales of each job id.
v. Display names and corresponding job titles of those employees who have singh (anywhere) in their
names.
vi. Change the jobid to 104 of the employee with id as E4 in the table employee.
vii. Change the data type of column name of employee table to varchar 30.

19 | P a g e
Solution:
Creating table and inserting values

20 | P a g e
i. Select max(sales) from employee group by jobid;

21 | P a g e
ii. Select job.jobid,
employeeid, name from job, employee where job.jobid = employee.jobid and sales>1350000;

iii. Display employee id, names of employees, job id with corresponding job titles.

iv. Display the minimum sales of each job id.

v. Display names and corresponding job titles of those employees who have singh (anywhere) in their
names.

22 | P a g e
vi. Change the jobid to 104 of the employee with id as E4 in the table employee.

vii. Change the data type of column name of employee table to varchar 30.

23 | P a g e
Practical Number 6
In a database there are two tables Product and Client with the following data. Write the outputs for SQL statements (i)
and (ii) and SQL statements for (iii) to (vii).

Table: Product
Structure:
Field Name Data Type Size Constraint
P_id Char 10 Primary Key
Productname Char 20
Manufacturer Char 10
Price Integer Not null
Data:
P_id Productname Manufacturer Price
P001 Moisturiser Xyz 40
P002 Sanitizer Tap 35
P003 Bath soap Cop 25
P004 Shampoo Tap 95
P005 Lens solution Cop 350
Table: Client
Structure:
Field Name Data Type Size Constraint
C_id Integer Primary Key
Clientname Char 20
City Char 20
P_id Char 10 Foreign key referencing
table Product column P_id
Data:
C_id Clientname City P_id
01 Dream Disney New Delhi P002
05 Life line Inc Mumbai P005
12 Dream line New Delhi P002
15 App care Bangalore P003
11 Lizel Mumbai P003
i. Select manufacturer, sum(price) from product group by manufacturer; ii. Select Product.p_id,
productname, c_id, Clientname from product, client where product.p_id = client.p_id and city=’New
delhi’; iii. Display the details of product whose price is in the range of 40 and 120 (both values
included).
iv. Display the product id, client id, manufacturer and client name of those clients who are from
Mumbai.
v. Display the client id and client name of those clients who name starts with word ‘dream’.

24 | P a g e
vi. Increase the price by 20 of all the products manufactured by Tap. vii. Remove the records of those
clients who are from Bangalore.

Solution:
Creating table and inserting values

25 | P a g e
i. Select manufacturer, sum(price) from product group by manufacturer;

ii. Select Product.p_id, productname, c_id, Clientname from product, client where product.p_id =
client.p_id and city=’New delhi’;

iii. Display the details of product whose price is in the range of 40 and 120 (both values included).

iv. Display the product id, client id, manufacturer and client name of those clients who are from
Mumbai.

v. Display the client id and client name of those clients who name starts with word ‘dream’.

26 | P a g e
vi. Increase the price by 20 of all the products manufactured by Tap.

vii. Remove the records of those clients who are from Bangalore.

27 | P a g e
Practical Number 7
Create the following table using SQL in MySQL database and perform the following queries:
Table: student Structure:
COLUMN NAME DATA TYPE CONSTRAINT
SID Integer Primary key
SNAME Char(30) Not null
GENDER Char(9)
ADDRESS Char(20)
MARKS Integer
DOB Date Not null
HOUSE_NAME Char(20)

Data:
SID SNAME GENDER ADDRESS MARKS DOB HOUSE_NAME
1 Anamika Sharma Female Nerul 89 2002-3-23 Red
2 Raj Kapoor Male Vashi 66 2003-5-5 Blue
3 Priya Bawde Female Vashi 78 2005-9-29 Red
4 Geeta Sharma Female Nerul NULL 2003-12-30 Green
5 Suraj Arora Male Sanpada 93 2001-1-15 Yellow
6 Deepak Saxena Male Nerul 44 2002-6-17 NULL
7 Seema Bawde Female Sanpada 50 2001-11-20 Green
8 Priyanshu Gupta Male Sanpada NULL 2004-10-13 Yellow
9 Prithvi Kapoor Male Nerul 40 2001-10-4 Blue
10 Seeta Parekh Female Vashi 50 2005-6-22 Blue

1. Display those students records who were born in the year 2001.
2. Display all the unique addresses.
3. Display student name and student id of those students who were awarded marks in the range 60 to 90.
4. Display the details of those students whose marks column is not empty.
5. Display the students name and address sorted in descending order of marks.
6. Display the details of those students whose name ends with ‘a’.
7. Display the students details arranged in ascending order of date of birth.
8. Display the name of month of each student they were born in.
9. Display the first letter of gender column for each student.
10. Find the maximum marks scored by student in each house.
11. Find the total number of students address wise.
12. Find the average marks scored by student in each house.
13. Display the average marks of each student house type having marks greater than 50.
14. Display the SID and SNAME of those student whose surname is Sharma or Bawde.
15. Add a new column mobile_number with data type char of size 10.
16. Change the name of column SNAME to S_NAME.
28 | P a g e
17. Award 10 marks to the students whose marks column is empty.
18. Increase the marks by 5 for those students who were born in the month of October.
19. Remove those students who are from yellow house.
20. Remove the column of address from the table student

Solution:

Creating table and inserting values

1. Display those students records who were born in the year 2001.

2. Display all the unique addresses.

29 | P a g e
3. Display student name and student id of those students who were awarded marks in the range 60 to 90.

4. Display the details of those students whose marks column is not empty.

5. Display the students name and address sorted in descending order of marks.

30 | P a g e
6. Display the details of those students whose name ends with ‘a’.

7. Display the students details arranged in ascending order of date of birth.

31 | P a g e
8. Display the name of month of each student they were born in.

9. Display the first letter of gender column for each student.

10. Find the maximum marks scored by student in each house.

32 | P a g e
11. Find the total number of students address wise.

12. Find the average marks scored by student in each house.

13. Display the average marks of each student house type having marks greater than 50.

14. Display the SID and SNAME of those student whose surname is Sharma or Bawde.

15. Add a new column mobile_number with data type char of size 10.

16. Change the name of column SNAME to S_NAME.

33 | P a g e
17. Award 10 marks to the students whose marks column is empty.

18. Increase the marks by 5 for those students who were born in the month of October.

19. Remove those students who are from yellow house.

20. Remove the column of address from the table student

Practical Number 8
Create a menu driven application with details of employees like employee id, name and salary.

Create a table employee with following columns in MySQL:

Perform the following operations:


a. Add a new employee with his/her id, name and salary.
b. View maximum and minimum salary of an employee.
c. Delete an employee based on his/her id provided by user. Provide error message if employee
doesn’t exist otherwise delete that employee from the table.
d. Show data of all the employees arranged in ascending order of salary.

Solution:
Python Code:

34 | P a g e
import mysql.connector
db = mysql.connector.connect(host="localhost",user="root",passwd="root",database="journal") cur
= db.cursor()
cur.execute("create table employees(eid varchar(20),ename varchar(20),salary int(11));")
db.commit() while True:
print("******************************\n1) Add a new employee with his/her id,name and salary.\n2) View maximum and
minimum salary of an employee.\n3) Delete an employee based on his/her id provided by user.\n4) Show data of all the employees
arranged in ascending order of salary.\n5) Exit\n******************************") a = int(input("Enter choice: ")) if a==1:
eid = input("Enter the employee id: ")
name = input("Enter the name: ") salary
= int(input("Enter the salary: "))
cur.execute(f"insert into employees values('{eid}','{name}','{salary}')")
db.commit()
print(cur.rowcount,"Record inserted successfully")
elif a==2:
cur.execute("select max(salary),min(salary) from employees;")
rs = cur.fetchall() for i in rs: print(i) elif a==3:
eid = input("Enter the employee id: ")
cur.execute("select eid from employees;")
rs = cur.fetchall() count = int() for i in
rs:
for j in i:
if eid==j:
cur.execute(f"delete from employees where eid='{eid}'")
db.commit()
count = 1
print("Record deleted successfully!")
if count==0:
print("No such employee id found in the table.")
elif a==4:
cur.execute("select * from employees order by salary;")
rs = cur.fetchall() for i in rs: print(i) elif a==5:
exit() else:
("Invalid Input")

35 | P a g e
Output:

36 | P a g e
Practical Number 9
Create a menu driven application which contains data about products like product id, name, quantity, price.

Create a table product with following columns in MySQL:

37 | P a g e
Table product should have the following data already existing:

Perform the following operations:


a. Search all the products starting from the given alphabet entered by the user at run time.
b. Search all the products whose price is less than or equal to the price entered by the user at run time.
c. Update the product details in product table based on product id entered by user at run time. Provide
error message if product doesn’t exist otherwise update the product details entered by the user.
d. Delete a product based on its id provided by user. If that product doesn’t exist then give an error
message otherwise remove that product.

Solution:
Python Code:
import mysql.connector
mydb =
mysql.connector.connect(host="localhost",user="root",passwd="root",database="practical")
mycursor = mydb.cursor() while True:
print("Product Details")
print("1. Search for all products with starting alphabet.")
print("2. Search for all products whose price is less than or equal to range
given.") print("3. Update the product details based on product id.") print("4.
Delete a product based on product id.") print("5. Exit")
c = int(input("Enter your choice: "))
if c==1:
name = input("Enter the alphabet: ")
sql = "select * from product where name like '{}
%'".format(name) mycursor.execute(sql) myresult =
mycursor.fetchall()
if mycursor.rowcount==0:
print("No matching results found.")
else:

38 | P a g e
print("Records are: ")
for i in myresult:
print(i)
elif c==2:
price = int(input("Enter price:"))
sql = "select * from product where
price<={}".format(price) mycursor.execute(sql)
myresult = mycursor.fetchall() if mycursor.rowcount==0:
print("No matching results found.")
else:
print("Records are: ")
for i in myresult:
print(i)
elif c==3:
pid = input("Enter product id: ")
name = input("Enter product name: ")
quantity = int(input("Enter quantity: "))
price = int(input("Enter price:"))
sql = "update product set name='{}',quantity={},price={} where
product_id='{}'".format(name,quantity,price,pid) mycursor.execute(sql) if mycursor.rowcount==0:
print("Record doesn't
exist.") else:
print("Record updated.") elif
c==4:
pid = int(input("Enter product id: "))
sql = "delete from product where
product_id='{}'".format(pid) mycursor.execute(sql)
mydb.commit() if mycursor.rowcount==0:
print("No matching results found.")
else:
print("Record deleted")

elif c==5:
exit() else:
print("Invalid Input")

Output:

39 | P a g e
Practical Number 10
Create a menu driven application which contains data about customer like customer id, name and country.

Create a table customer with following columns in MySQL:

40 | P a g e
Table customer should have the following data already existing:

Perform the following operations:


a. Show total number of customers arranged country wise.
b. Search all the customers of a particular country where country name is entered by user at run time.
Give error message if no record is found.
c. Update the customer details in customer table based on customer id entered by user at run time.
Provide error message if customer id doesn’t exist otherwise update the customer details entered by
the user.
d. Add a new customer with his details as cid, cname, gender, country.

Solution:
Python Code:
import mysql.connector
db =
mysql.connector.connect(host='localhost',user='root',passwd='root',database='journal2')
cur = db.cursor() while True:
print("******************************\n1) Show total number of customers arranged country wise.\n2) Search all the
customers of a particular country wise\n3) Update the customer details in customer table based on customer id\n4) Add a
new customer with his details as cid, cname, gender, country\n5 Exit\n******************************") a =
int(input("Enter choice: ")) if a==1:
cur.execute("select count(*), country from customer group by
country;") rs = cur.fetchall() for i in rs: print(i) elif a==2:
country = input("Enter country name: ")
cur.execute("select distinct country from
customer;") rs = cur.fetchall() count = int()
for i in rs: for j in i: if
country.upper()==j.upper():
cur.execute(f"select * from customer where
country='{country}'") count = 1 y = cur.fetchall()
for x in y: print(x) if count==0:
41 | P a g e
print("No such country found in the table.")
elif a==3:
cid = input("Enter the customer id: ")
cur.execute("select cid from customer;")
rs = cur.fetchall()
if(cid,) in
rs:
name = input("Enter new customer name: ")
gender = input("Enter new gender: ")
country = input("Enter new country: ")
qs = "update customer set cname='{}',gender='{}',country='{}' where
cid='{}'".format(name,gender,country,cid) cur.execute(qs) db.commit()
print("Record successfully updated!")
else:
print("No matching record found!")
elif a==4:
cid = input("Enter the customer id: ")
name = input("Enter the name: ")
gender = input("Enter the gender: ")
country = input("Enter the country: ")
cur.execute(f"insert into customer values('{cid}','{name}','{gender}','{country}')")
db.commit()
print(cur.rowcount," Record inserted
successfully") elif a==5: exit(0)

Output:

42 | P a g e
Practical Number 11
Create a menu driven application for parking system to do the following:

Create a table parking with following columns in MySQL:

43 | P a g e
Perform the following operations:

a. Add a record with vehicle no, vehicle type, owner name taken from user at run time. In date time
should be the current system date and time which should be automatically inserted.
b. Update a record on the basis of vehicle no entered by the user. If vehicle no doesn’t exist then give
error message otherwise update the record of that vehicle with current system time as out date time.
c. Delete a record on the basis of vehicle no entered by the user. It vehicle no doesn’t exist then given
error message otherwise delete that vehicle details from parking table. d. Show all the records.

Solution:
Python Code:
import mysql.connector
obj = mysql.connector.connect(host="localhost",user="root",passwd="root",database="journal")
cur = obj.cursor()
cur.execute("create table parking(vehicleno varchar(20),vehicletype varchar(20),ownername varchar(20),in_date_time
datetime default now(),out_date_time datetime);") obj.commit() while True: print('''Select:
1) Add a record with vehicle no, vehicle type,
owner name
2) Update a record on the basis of vehicle no
3) Delete a record on the basis of vehicle no
4) Show all records
5) Exit''')
c = int(input("Enter Choice: "))
if c==1:
vn = input("Enter vehicle number: ") vt = input("Enter vehicle type: ") on =
input("Enter owner name: ") cur.execute(f"insert into
parking(vehicleno,vehicletype,ownername) values('{vn}','{vt}','{on}')") obj.commit() elif
c==2:
vn = input("Enter the veicle no: ")
cur.execute("select vehicleno from
parking;") rs = cur.fetchall() if (vn,) in rs:
vt = input("Enter Vehicle Type: ")
on = input("Enter OwnerName: ")
cur.execute(f"update parking set
ownername='{on}',vehicletype='{vt}'
where vehicleno='{vn}'")
obj.commit()
print("Record updated successfully!")
else:
print("No such vehicle number found in the table")
elif c==3:

44 | P a g e
vn = input("Enter the vehicle number: ")
cur.execute("select vehicleno from parking;")
rs = cur.fetchall() if (vn,) in rs:
cur.execute(f"delete from parking where vehicleno='{vn}';")
obj.commit()
print("Record deleted successfully!")
else:
print("No such Record found")
elif c==4:
cur.execute("select * from parking;")
rs = cur.fetchall()
print(rs)
elif c==5:
cur.execute("select now()")
rs =
cur.fetchall() for
i in rs: for j in
i: y=j
cur.execute(f"update parking set out_date_time='{y}' where out_date_time is NULL")
obj.commit()
exit(0)

Output:

45 | P a g e
Practical Number 12
Create the following GUI application using tkinter library of Python.

46 | P a g e
Create a table customer with following columns in MySQL:

Perform the following operations:


a. On click of ADD RECORD button, a new record should be added in the customer table.
b. On click of CLEAR button all the text fields contents should be cleared.

Solution:
Python Code:
import tkinter import
mysql.connector window =
tkinter.Tk()
window.title("CUSTOMER
DETAILS")
obj =
mysql.connector.connect(host="localhost",user="root",passwd="root",database="practical") cur
= obj.cursor() global l5 def insert(): global l5 #to add record cid = t1.get() cname =
t2.get() gender = t3.get() country = t4.get()
sql = "insert into customer
values('{}','{}','{}','{}')".format(cid,cname,gender,country) cur.execute(sql)
obj.commit()
l5 = tkinter.Label(text="RECORD ADDED",font=("Arial Bond",10))
l5.grid(column=0,row=6)

def clear():
#to clear the contents in text field
global l5
t1.delete(0,'end')
t2.delete(0,'end')
t3.delete(0,'end')
t4.delete(0,'end')
l5.destroy()

47 | P a g e
l1 = tkinter.Label(text="ENTER CUSTOMER ID",font=("Arial Bold",10)).grid(column=0,row=0) l2
= tkinter.Label(text="ENTER CUSTOMER NAME",font=("Arial Bold",10)).grid(column=0,row=1)
l3 = tkinter.Label(text="ENTER GENDER",font=("Arial Bold",10)).grid(column=0,row=2) l4 =
tkinter.Label(text="ENTER COUNTRY",font=("Arial Bold",10)).grid(column=0,row=3)

t1 = tkinter.Entry(window,width =
20) t1.grid(column=1,row=0) t2 =
tkinter.Entry(window,width = 20)
t2.grid(column=1,row=1) t3 =
tkinter.Entry(window,width = 20)
t3.grid(column=1,row=2) t4 =
tkinter.Entry(window,width = 20)
t4.grid(column=1,row=3)

b1 = tkinter.Button(window,text="ADD RECORD",bg="orange",fg="red",command=insert)
b1.grid(column=0,row=4)
b2 = tkinter.Button(window,text="CLEAR",bg="orange",fg="red",command=clear)
b2.grid(column=0,row=5)

window.mainloop()

Output:

Practical Number 13
Write a menu driven program for linear list with the following options:

1. Traverse

48 | P a g e
2. Search
3. Insert
4. Delete
5. Sort
6. Exit

Solution:
Python Code:
def traverse():
#Code to traverse
print("The Elements of the list are: ")
for i in range(len(a)):
print(a[i])

def search():
#Code to search
item = int(input("Enter element to search:
")) c = 0 if item in a:
print("Item found at position
",a.index(item)) c = 1 if c==0:
print("Item not found")

def insert():
#Code to insert
item = int(input("Enter element to insert: "))
a.append(item)
print("Item added sucessfully")

def delete():
#Code to delete
item = int(input("Enter element to delete:
")) c = 0 b = a.count(item) if b!=0:
for i in range(b):
a.remove(item)
c+=1 else:
print("Item not found") if
c!=0:
print("Item deleted sucessfully")

def sort():
#Code to sort
a.sort()
print("Sorted list is: ",a)

size = int(input("Enter size of linear list: "))

49 | P a g e
a = [None]*size
for i in range(size):
a[i] = int(input("Enter elements: "))
while True:
print("Select any option:")
print("1. Traverse\n2. Search\n3. Insert\n4. Delete\n5. Sort\n6.
Exit") c = int(input("Enter Choice: ")) if c==1: traverse() elif
c==2: search() elif c==3: insert() elif c==4: delete()
elif c==5: sort() elif c==6: exit(0) else:
print("Invalid Input")

Output:

50 | P a g e
51 | P a g e
Practical Number 14
Write a Python program to input two lists and return a list that contains only the elements that are common between the
lists. No element should be repeated. Make use of list comprehensions.

Solution:
Python Code:
l1 = eval(input("Enter List 1: "))
l2 = eval(input("Enter List 2: "))
l3 = [i for i in l1 if i in l2]
print("The List of common elements: ",l3)

Output:

52 | P a g e
Practical Number 15
Write a menu driven program for stack with the following options:

1. Push 2.
Pop
3. Peek
4. Display
5. Exit

Solution:
Python Code:
L=[] def push(L):
a=int(input("Enter element:"))
L.append(a) def pop(L): if
L==[]: print("Underflow
error") else:
print(L.pop()) def
peek(L): if L==[]:
print("Underflow error")
else:
print(L[-1])
def display(L):
print(L) while
True:
print("Select any option:\n1. Push \n2. Pop \n3. Peek \n4. Display \n5. Exit")
ch=int(input("Enter choice:")) if ch==1: push(L) elif ch==2: pop(L)
elif ch==3: peek(L) elif ch==4: display(L) elif ch==5: exit()

Output:

53 | P a g e
54 | P a g e

You might also like