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

Corrected Code Document Updated

The document contains various practical programming questions and SQL commands related to stack operations, database table creation, and data storage in files. It includes Python programs for managing book and bank details, as well as SQL commands for creating and manipulating train, reservation, and fee tables. Additionally, it demonstrates how to store loan details in a binary file using Python's pickle module.

Uploaded by

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

Corrected Code Document Updated

The document contains various practical programming questions and SQL commands related to stack operations, database table creation, and data storage in files. It includes Python programs for managing book and bank details, as well as SQL commands for creating and manipulating train, reservation, and fee tables. Additionally, it demonstrates how to store loan details in a binary file using Python's pickle module.

Uploaded by

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

Corrected Code Document

Practical Question 1
Python Program for Stack Operations:

class BookStack:
def __init__(self):
self.stack = []

def push(self, book):


self.stack.append(book)
print(f'"{book}" has been added to the stack.')

def pop(self):
if not self.stack:
print("The stack is empty. Nothing to pop.")
else:
removed_book = self.stack.pop()
print(f'"{removed_book}" has been removed from the stack.')

def traverse(self):
if not self.stack:
print("The stack is empty.")
else:
print("Books in the stack:")
for book in reversed(self.stack):
print(book)

# Menu-driven interface
stack = BookStack()
while True:
print("\nMenu:\n1. Push a Book\n2. Pop a Book\n3. Traverse Stack\n4. Exit")
try:
choice = int(input("Enter your choice: "))
if choice == 1:
book_name = input("Enter the name of the book: ")
stack.push(book_name)
elif choice == 2:
stack.pop()
elif choice == 3:
stack.traverse()
elif choice == 4:
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")

Practical Question 2
SQL Commands for Books Table:

-- Create the book table


CREATE TABLE book (
bookid INT PRIMARY KEY,
bookname CHAR(10),
book_price INT
);

-- Insert 5 rows into the table


INSERT INTO book VALUES
(1, 'Shadows', 300),
(2, 'Secrets', 450),
(3, 'Sculpture', 500),
(4, 'Python', 600),
(5, 'SQLGuide', 700);

-- Display all book names starting with 'S'


SELECT bookname FROM book WHERE bookname LIKE 'S%';

Practical Question 3
Python Program for Bank Details Storage in Text File:

bank_details = []
n = int(input("Enter the number of bank records: "))

for _ in range(n):
accno = input("Enter Account Number: ")
name = input("Enter Name: ")
bal = input("Enter Balance: ")
bank_details.append(f"{accno},{name},{bal}\n")
with open("bank.txt", "w") as file:
file.writelines(bank_details)

print("Bank details stored in bank.txt.")

with open("bank.txt", "r") as file:


print("\nBank Details:")
print(file.read())

Practical Question 4
SQL Commands for Train and Reservation Tables:

-- Create the train table


CREATE TABLE train (
pnrno INT PRIMARY KEY,
pass_name CHAR(10),
ticket_price INT
);

-- Insert 5 rows into the train table


INSERT INTO train VALUES
(101, 'Alice', 500),
(102, 'Bob', 600),
(103, 'Charlie', 700),
(104, 'Diana', 800),
(105, 'Eve', 900);

-- Create the reservation table


CREATE TABLE reservation (
pnrno INT,
source CHAR(20),
dest CHAR(20),
journey_dt DATE
);

-- Insert 5 rows into the reservation table


INSERT INTO reservation VALUES
(101, 'Delhi', 'Mumbai', '2025-01-10'),
(102, 'Kolkata', 'Chennai', '2025-01-11'),
(103, 'Bangalore', 'Hyderabad', '2025-01-12'),
(104, 'Pune', 'Ahmedabad', '2025-01-13'),
(105, 'Goa', 'Jaipur', '2025-01-14');

-- Display passenger name, source, and journey date


SELECT train.pass_name, reservation.source, reservation.journey_dt
FROM train
JOIN reservation ON train.pnrno = reservation.pnrno;

Practical Question 5
Python Program for Displaying Books with Price Greater Than 500 Using MySQL:

import mysql.connector

try:
# Establishing connection
conn = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="lib"
)
cursor = conn.cursor()

# Query to fetch books with price > 500


query = "SELECT * FROM library WHERE book_price > 500;"
cursor.execute(query)

# Displaying results
print("Books with price greater than 500:")
for row in cursor.fetchall():
print(row)

except mysql.connector.Error as e:
print(f"Error: {e}")

finally:
if conn.is_connected():
cursor.close()
conn.close()

Practical Question 6
SQL Commands for Fee Table:
-- Create the fee table
CREATE TABLE fee (
studid INT,
studname CHAR(10),
stud_fee INT
);

-- Insert 5 rows into the table


INSERT INTO fee VALUES
(1, 'John', 2000),
(2, 'Jane', 2500),
(NULL, 'Mike', 3000),
(4, 'Lucy', 2200),
(5, 'Emma', 2800);

-- Update all stud_fee to 0 where studid is NULL


UPDATE fee SET stud_fee = 0 WHERE studid IS NULL;

Practical Question 7
Python Program for Loan Details Storage in a Binary File:

import pickle

loan_details = []
n = int(input("Enter the number of loan records: "))

for _ in range(n):
loanno = int(input("Enter Loan Number: "))
name = input("Enter Name: ")
loan_amt = float(input("Enter Loan Amount: "))
loan_details.append({"loanno": loanno, "name": name, "loan_amt": loan_amt})

with open("loan.dat", "wb") as file:


pickle.dump(loan_details, file)

print("Loan details stored in loan.dat.")

with open("loan.dat", "rb") as file:


loans = pickle.load(file)
print("\nLoan Details:")
for loan in loans:
print(loan)

You might also like