0% found this document useful (0 votes)
9 views

CS- Practical (2023-24) - Copy (1)

Uploaded by

demon121869
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CS- Practical (2023-24) - Copy (1)

Uploaded by

demon121869
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

C

1. Write a Python program to read characters one by one and store


lowercase letters in the file "lower", store uppercase letters in the file
"upper" and store other characters in the file "other".

2. Create the following table and execute SQL commands.

Item Item name Quantity Price Company Trade


Code Code
1001 Pen drive 640 1200 HP T01
64GB
1004 Digital cam 70 14000 CANON T02
1002 Car GPS 50 1250 GEOKNOW T01
System
1005 LED Screen 190 38000 LG T02
40inch
1007 Digital 600 11000 XENTA T03
pad121
Table: ITEM

Table: TRADES
TRADE CODE TNAME CITY
T01 Electronics Sales Mumbai
T02 Busy Store Corp Delhi
T03 Disp House Inc Chennai

1. To display details of all items in ascending order of item names.


2. To display item name and price of all those items, whose price is in
the range of 10000 and 20000
3. To display the number of items, which is trade by each trader?
4. To display the price, item name and quantity of those items which
have more than 150.
5. To display the name of those traders, who is either from
Delhi/Mumbai?
B
1. Write a python program to implement a stack operation. (Perform Push,
Pull, View, and Peek)

2. Write a python program to integrate SQL with python by importing the


MYSQL module to search a student using roll no. and delete the record

1. Write a python program to remove all the line that contain the character
in a file and write it to another file.

2. Create a student Table and Insert Data to implement the following SQL
comments.
i. ALTER TABLE to add new attributes / modify data type /drop
attribute.
ii. UPDATE TABLE to modify data.
iii. ORDER BY to display data in ascending / descending order.
iv. Find the min, max, sum, count and average.
\
file = open("info.txt", "r")

print('Content of the file :')

print(file.read())

file.seek(0)

lines = file.readlines()

new_lines = []

for line in lines:

if "a" not in line.strip():

new_lines.append(line)

file.close()

file = open("newinfo.txt", "w")

file.writelines(new_lines)

file.close()

with open('newinfo.txt','r') as fn:

content = fn.read()

print('Content of the new file after removing the lines containing


chr "a" :')

print(content)
import mysql.connector

# Function to connect to the MySQL database


def connect_to_db():
try:
connection = mysql.connector.connect(
host="localhost", # Replace with your host if different
user="root", # Replace with your MySQL username
password="password", # Replace with your MySQL password
database="school" # Replace with your database name
)
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None

# Function to search a student by roll number


def search_student_by_roll_no(connection, roll_no):
try:
cursor = connection.cursor()
query = "SELECT * FROM students WHERE roll_no = %s"
cursor.execute(query, (roll_no,))
result = cursor.fetchone() # Fetching one record
if result:
print("Student Found: ", result)
return True
else:
print("Student not found.")
return False
except mysql.connector.Error as err:
print(f"Error: {err}")
return False

# Function to delete a student by roll number


def delete_student_by_roll_no(connection, roll_no):
try:
cursor = connection.cursor()
query = "DELETE FROM students WHERE roll_no = %s"
cursor.execute(query, (roll_no,))
connection.commit() # Committing the changes to the database
print(f"Student with roll number {roll_no} has been deleted.")
except mysql.connector.Error as err:
print(f"Error: {err}")
connection.rollback() # Rolling back in case of error

# Main program
def main():
roll_no = input("Enter the roll number of the student you want to search and delete: ")

# Connect to the database


connection = connect_to_db()
if connection:
# Search the student
if search_student_by_roll_no(connection, roll_no):
# Confirm deletion
confirm = input(f"Do you really want to delete the record of roll number {roll_no}? (yes/no): ")
if confirm.lower() == 'yes':
delete_student_by_roll_no(connection, roll_no)
connection.close() # Close the connection to the database
else:
print("Failed to connect to the database.")

if __name__ == "__main__":
main()
-- 1. Create Tables
CREATE TABLE ITEM (
item_code INT PRIMARY KEY,
item_name VARCHAR(100),
quantity INT,
price INT,
company VARCHAR(100),
trade_code VARCHAR(10)
);

CREATE TABLE TRADES (


trade_code VARCHAR(10) PRIMARY KEY,
tname VARCHAR(100),
city VARCHAR(100)
);
-- 2. Insert Data into ITEM Table
INSERT INTO ITEM (item_code, item_name, quantity, price, company,
trade_code)
VALUES
(1001, 'Pen drive 64GB', 640, 1200, 'HP', 'T01'),
(1004, 'Digital cam', 70, 14000, 'CANON', 'T02'),
(1002, 'Car GPS System', 50, 1250, 'GEOKNOW', 'T01'),
(1005, 'LED Screen 40inch', 190, 38000, 'LG', 'T02'),
(1007, 'Digital pad121', 600, 11000, 'XENTA', 'T03');

-- 3. Insert Data into TRADES Table


INSERT INTO TRADES (trade_code, tname, city)
VALUES
('T01', 'Electronics Sales', 'Mumbai'),
('T02', 'Busy Store Corp', 'Delhi'),
('T03', 'Disp House Inc', 'Chennai');

-- 4. SQL Queries
-- 4.1 Display all items in ascending order of item names
SELECT * FROM ITEM
ORDER BY item_name ASC;

-- 4.2 Display item name and price where price is between 10000 and
20000
SELECT item_name, price FROM ITEM
WHERE price BETWEEN 10000 AND 20000;

-- 4.3 Number of items traded by each trader


SELECT trade_code, COUNT(*) AS num_of_items
FROM ITEM
GROUP BY trade_code;
-- 4.4 Price, item name, and quantity of items with quantity > 150
SELECT price, item_name, quantity FROM ITEM
WHERE quantity > 150;

-- 4.5 Name of traders from Delhi or Mumbai


SELECT tname
FROM TRADES
WHERE city IN ('Delhi', 'Mumbai');

You might also like