0% found this document useful (0 votes)
8 views7 pages

12th STD Computer Science HANDS On PRACTICE Chapter-15

The document provides hands-on practice programs for creating a customer database and querying game records using Python and SQLite. It includes code snippets for accepting user input, storing data in CSV files, and executing SQL queries to display game details based on various criteria. The document is prepared by J. Sasirekha and is intended for educational purposes on a YouTube channel named 'CS Aim Higher'.
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)
8 views7 pages

12th STD Computer Science HANDS On PRACTICE Chapter-15

The document provides hands-on practice programs for creating a customer database and querying game records using Python and SQLite. It includes code snippets for accepting user input, storing data in CSV files, and executing SQL queries to display game details based on various criteria. The document is prepared by J. Sasirekha and is intended for educational purposes on a YouTube channel named 'CS Aim Higher'.
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/ 7

YOU TUBE CHANNEL NAME:CS Aim higher 1

CHAPTER 15 HANDS ON PRACTICE PROGRAMS


1. Create an interactive program to accept the details
from user and store it in a csv file using Python for the
following table?
Answer:
Database name;- DB1
Table name : Customer

import sqlite3 import io import csv


d = open(‘c:/pyprg/sql.csv’, ‘w’) c = csv.writer(d)
connection = sqlite3.connect(“dbl.db”)
cursor = connection.cursor( )
cursor.execute(“create table customer(cust_Id,
cust_Name, Address, Phone_no, City)”)
print(“Enter 3 customer details:”)
print(“Enter 3 customer Id :”)
cid = [int(input( )) for i in range (3)]
print(“Enter customer names :”)
cname = [input( ) for i in range (3)]
print(“Enter their Address:”)
add = [input( ) for i in range (3)]
int(“Enter their phone numbers:”)

PREPARED BY:J.SASIREKHA,KATPADI
YOU TUBE CHANNEL NAME:CS Aim higher 2

ph = [int(input( )) for i in range (3)]


print(“Enter their cities:”)
city = [input( ) for i in range (3)]
n = len(cname)
for i in range (n):
cursor.execute(“insert into customer values (?,?,?,?,?)”,
(cid[i], cname[i], add[i], ph[i], city[i]))
cursor.execute(“Select * from customer “)
co = [i[0] for i in cursor, description]
c.writerow(co)
data = cursor. fetchall( )
for item in data:
c.writerow(item)
– d.close( )
with open(‘c:/pyprg/sql.csv’, “r”, newline = None) as fd:
for line in fd:
line = line.replace(“\n”,” “)
print(line)
cursor. close( )
connection. close( )

OUTPUT:

Enter 3 customer details:


Enter 3 customer Id:
C008
C010
C012
PREPARED BY:J.SASIREKHA,KATPADI
YOU TUBE CHANNEL NAME:CS Aim higher 3

Enter customer names:

Sandeep
Anurag Basu

Hrithik

Enter their Address:


14/1 Pritam Pura
15/A, Park Road
7/2 Vasant Nagar

Enter their Phone Numbers:


41206819
61281921
26121949

Enter their cities:


Delhi
Kolkata
Delhi

Displaying Data:
(‘cust_Id’, ‘cust_Name’, ‘Address’, ‘Phone_no’, ‘city’)
(C008, ‘Sandeep’, ’14/1 Pritampura’, ‘41206819’, ’Delhi’)
(C010, ‘Anurag Basu’, ’15A, Park Road’, ’61281921’,
’Kolkata’)
(C012, ’Hrithik’, ’7/2 Vasant Nagar’, ’26121949’, ’Delhi’)

PREPARED BY:J.SASIREKHA,KATPADI
YOU TUBE CHANNEL NAME:CS Aim higher 4

2.Consider the following table GAMES. Write a python


program to display the records for question (i) to (iv) and
give outputs for SQL queries (v) to (viii)?
Table: GAMES

1. To display the name of all Games with their


Geodes in descending order of their schedule date.
2. To display details of those games which are having
Prize Money more than 7000.
3. To display the name and gamename of the Players
in the ascending order of Gamename.
4. To display sum of PrizeMoney for each of the
Numberof participation groupings (as shown in
column Number 4)
5. Display all the records based on GameName

PREPARED BY:J.SASIREKHA,KATPADI
YOU TUBE CHANNEL NAME:CS Aim higher 5
(i). To display the name of all Games with their

Geodes in descending order of their schedule date.


import sqlite3
conn = sqlite3.connect(“Games.db”)

cursor = conn.cursor( )
cursor.execute(“Select GameName, Geode from Games
order by ScheduleDate Desc “)
result = cursor,fetchall( )
print(*result, sep = “\n”)
conn.close( )

Output:
(’Table Tennis’, 108)
(’Table Tennis’, 103)
(’Carom Board’, 101)
(’Carom Board’, 105)
(’Badminton1, 102)

(ii). To display details of those games which are having


Prize Money more than 7000.
import sqlite3
conn = sqlit3.connect(“Games.db”)
cursor = conn.cursor( )
cursor.execute(” Select * from Games where prize money >
7000″)
result = cursor.fetchall( )
print(*result, sep = “\n”)
conn.close( )

PREPARED BY:J.SASIREKHA,KATPADI
YOU TUBE CHANNEL NAME:CS Aim higher 6
Output:
(102, ‘Vidhya’, ‘Badminton’, 2, 12000, ’12-12-2013′)
(103, ‘Guru’, ’Table Tennis’, 4, 8000, ’02-14-2014’)
(105, ‘Keerthana’, ‘Carom Board’, 2, 9000, ’01-01-2014′)
(108, ’Krishna’, ’Table Tennis’, 4, 25000, ’03-19-2014′)

(iii) To display the name and gamename of the Players in


the ascending order of Gamename.
import sqlite3
conn = sqlite3.connect(“Games.db”)
cursor = conn.cursor( )
cursor.execute(” Select Name, GameName from games
order by GameName “)
result = cursor.fetchall( )
print(*result, sep = “\n”)
conn.close( )

Output:
(‘Vidhya’, ‘Badminton’)
(‘Padmaja’, ‘Carom Board’)
(‘Keerthana’, ‘Carom Board’)
(‘Guru’, ‘Table Tennis’)
(‘Krishna’, ‘Table Tennis’)

(iv) To display sum of PrizeMoney for each of the Numberof


participation groupings (as shown in column Number 4)
import sqlite3
conn = sqlite3.connect(“Games.db”)

PREPARED BY:J.SASIREKHA,KATPADI
YOU TUBE CHANNEL NAME:CS Aim higher 7
cursor = conn.cursor( )
cursor.execute(“Select Sum(Number * Prizemoney) from
games”)
result = cursor, fetchall( )
print(result)
conn.close( )

Output:
[(184000)]

(v) Display all the records based on GameName


import sqlite3
conn = sqlite3.connect(“Games.db”)
cursor = conn.cursor( )
cursor.execute(“Select * from games group by gamename”)
result = cursor. fetchall( )
print(*result, sep = “\n”)
conn.close( )

Output:
(‘Carom Board’, 101, ‘Padmaja’, 2, 5000, ’01-23-2014′)
(’Carom Board’, 105, ‘Keerthana’, 2, 9000, ’01-01-2014′)
(‘Badminton’, 102, ‘Vidhya’, 2, 12000, ’12-12-2013′)
(‘Table Tennis’, 103, ‘Guru’, 4, 8000, ’02-14-2014′)
(’Table Tennis’, 108, ‘Krishna’, 4, 25000, ’03-19-2014′)

PREPARED BY:J.SASIREKHA,KATPADI

You might also like