Wikipedia

Search results

Tuesday, January 30, 2024

CLASS XII: PRACTICAL PROGRAMS

 1. Write a menu drive program to perform following operations into a binary file shoes.dat. 8

a) Add record b) Display records c) Search record d) Exit The structure of file content is: [s_id, name, brand, type, price]

Solution:
import pickle def add_record(): s_id = int(input("Enter Shoe ID: ")) name = input("Enter Name: ") brand = input("Enter Brand: ") shoe_type = input("Enter Type: ") price = float(input("Enter Price: ")) record = (s_id, name, brand, shoe_type, price) with open("shoes.dat", "ab") as file: pickle.dump(record, file) print("Record added successfully.") def display_records(): try: with open("shoes.dat", "rb") as file: while True: record = pickle.load(file) print("Shoe ID:", record[0]) print("Name:", record[1]) print("Brand:", record[2]) print("Type:", record[3]) print("Price:", record[4]) print("-" * 30) except EOFError: pass except FileNotFoundError: print("File not found. No records to display.") def search_record(): try: s_id = int(input("Enter Shoe ID to search: ")) with open("shoes.dat", "rb") as file: while True: record = pickle.load(file) if record[0] == s_id: print("Record found:") print("Shoe ID:", record[0]) print("Name:", record[1]) print("Brand:", record[2]) print("Type:", record[3]) print("Price:", record[4]) return except EOFError: pass except FileNotFoundError: print("File not found. No records to search.") def main(): while True: print("\nMenu:") print("a) Add record") print("b) Display records") print("c) Search record") print("d) Exit") choice = input("Enter your choice: ").lower() if choice == 'a': add_record() elif choice == 'b': display_records() elif choice == 'c': search_record() elif choice == 'd': print("Exiting program. Goodbye!") break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main()






2.
Solution:
import mysql.connector as mycon

# i. Fill in the parameters and values for statement 1 cn = mycon.connect(user='root', password='tiger', host='localhost',database='Customer')
# ii. Write function name to create a cursor and fill in the gap for statement 2 cr = cn.cursor() cust_id = int(input("Enter ID:")) cust_name = input("Enter Customer Name:") city = input("Enter City:") ba = float(input("Enter Bill Amount:")) mno = input("Enter Mobile No.") # iii. Write a query to fill statement 3 with desired values query = "INSERT INTO customer (CustomerID, CustomerName, City, BillAmt, MobileNo) VALUES (%s, %s, %s, %s, %s)" values = (cust_id, cust_name, city, ba, mno) cr.execute(query, values) # iv. Write a query to fill statement 4 to save the records into the table cn.commit() # Close the cursor and connection cr.close() cn.close()





2 comments:

  1. This blog post is really very informative. Where you are providing the information about “CLASS XII: PRACTICAL PROGRAMS” That is really such nice information for those who need it. Here in this post, you are also providing the information about “Write a menu drive program to perform following operations into a binary file shoes.dat” that is one of the most important information. But here my opinion is that you can have to create a website and spread your information to worldwide. So, that you can choose Ritz Media World for Web Design Company In Delhi NCR.

    ReplyDelete

  2. This blog post is really very informative. Where you are providing the information about “CLASS XII: PRACTICAL PROGRAMS” That is really such nice information for those who need it. Here in this post, you are also providing the information about " Write a menu drive program to perform following operations into a binary file shoes.dat" that is one of the most important information. I can also provide the information about the property and locality perspective, if someone need the information like Dholera Smart City Project or any other real esate related details they can connect with Prooperty.com


    ReplyDelete