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

Code CS Project

Uploaded by

manomani007123
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

Code CS Project

Uploaded by

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

CODE:

import mysql.connector as ms

# Connect to the MySQL database


con = ms.connect(host='localhost', user='root',
passwd='name12345', database='library_management')
cur = con.cursor()

def display_options():
"""Displays the available options for the user"""
print("The options are:")
print("1. Add book")
print("2. Search book")
print("3. Remove book")
print("4. Add customer")
print("5. Search customer")
print("6. Remove customer")
print("7. Exit")

def execute_query(query, params=()):


"""Utility function to execute a query and handle the result"""
cur.execute(query, params)
return cur.fetchall()

# Main loop for the program


while True:
display_options()
try:
# Get user input for option
opt = int(input("Enter the option (1,2,3,4,5,6,7): "))
except ValueError:
print("Invalid input. Please enter a number between 1 and
7.")
continue

if opt == 1: # Add book


def add_book():
bname = input('Enter the book name: ')
Author = input('Enter the author name: ')
try:
bprice = float(input('Enter the price of the book: '))
except ValueError:
print("Invalid price. Please enter a valid number.")
return
bcat = input('Enter the category of the book (Comic,
Story, Novel, Study Guide, Subject): ')

query = "INSERT INTO library (Bname, Author, Price,


Category) VALUES (%s, %s, %s, %s)"
cur.execute(query, (bname, Author, bprice, bcat))
con.commit()
print("Book added successfully.")

add_book()

elif opt == 2: # Search book


def search_book():
cat = input("Enter the category (Bookname, Novel,
Guide, Story, Subject, Comic): ").lower()
if cat == 'bookname':
bname1 = input('Enter book name to search: ')
query = "SELECT * FROM library WHERE Bname =
%s"
results = execute_query(query, (bname1,))
else:
query = "SELECT * FROM library WHERE Category =
%s"
results = execute_query(query, (cat,))

for record in results:


print(record)

search_book()

elif opt == 3: # Remove book


def remove_book():
bname1 = input("Enter the book name to remove: ")
query = "DELETE FROM library WHERE Bname = %s"
cur.execute(query, (bname1,))
con.commit()
print("Book deleted.")

remove_book()
elif opt == 4: # Add customer
def add_cust():
cname = input('Enter the Customer name: ')
bname = input('Enter the book name: ')
try:
bprice = float(input('Enter the price of the book: '))
except ValueError:
print("Invalid price. Please enter a valid number.")
return
bcat = input('Enter the category of the book (Comic,
Story, Novel, Study Guide, Subject): ')

query = "INSERT INTO customer (Cname, Bname, Price,


Category) VALUES (%s, %s, %s, %s)"
cur.execute(query, (cname, bname, bprice, bcat))
con.commit()
print("Customer added successfully.")

add_cust()

elif opt == 5: # Search customer


def search_cust():
cname1 = input('Enter Customer name to search: ')
query = "SELECT * FROM customer WHERE Cname =
%s"
results = execute_query(query, (cname1,))
for record in results:
print(record)

search_cust()

elif opt == 6: # Remove customer


def remove_cust():
cname1 = input('Enter the customer name to delete: ')
query = "DELETE FROM customer WHERE Cname = %s"
cur.execute(query, (cname1,))
con.commit()
print("Customer deleted.")

remove_cust()

elif opt == 7: # Exit


print("Thank you")
break

else:
print("Invalid option. Please choose a number between 1
and 7.")

# Close the database connection


cur.close()
con.close()
SQL CODE:
CREATE TABLE IF NOT EXISTS library (
Bname VARCHAR(255) NOT NULL,
Author VARCHAR(255) NOT NULL,
Price DECIMAL(10, 2) NOT NULL,
Category VARCHAR(100) NOT NULL,
PRIMARY KEY (Bname)
);

CREATE TABLE IF NOT EXISTS customer (


Cname VARCHAR(255) NOT NULL,
Bname VARCHAR(255) NOT NULL,
Price DECIMAL(10, 2) NOT NULL VARCHAR(100) NOT NULL,
PRIMARY KEY (Cname, Bname),
FOREIGN KEY (Bname) REFERENCES library(Bname) ON
DELETE CASCADE
);

INSERT INTO library (Bname, Author, Price, Category)


VALUES
('Python for Beginners', 'John Doe', 29.99, 'Programming'),
('The Great Adventure', 'Jane Smith', 19.99, 'Novel'),
('Introduction to Algorithms', 'Thomas H. Cormen', 59.99,
'Study Guide'),
('The Mystery of the Haunted House', 'Mark Twain', 12.99,
'Story'),
('Learn SQL Quickly', 'Alice Johnson', 24.99, 'Programming'),
('Machine Learning Basics', 'David Brown', 49.99,
'Technology'),
('The Art of War', 'Sun Tzu', 9.99, 'History'),
('Moby Dick', 'Herman Melville', 14.99, 'Classic'),
('The Great Gatsby', 'F. Scott Fitzgerald', 11.99, 'Classic'),
('1984', 'George Orwell', 15.99, 'Dystopian'),
('To Kill a Mockingbird', 'Harper Lee', 13.99, 'Classic'),
('Pride and Prejudice', 'Jane Austen', 16.99, 'Romance'),
('War and Peace', 'Leo Tolstoy', 25.99, 'Historical Fiction'),
('The Catcher in the Rye', 'J.D. Salinger', 10.99, 'Fiction'),
('A Brief History of Time', 'Stephen Hawking', 29.99,
'Science'),
('The Hobbit', 'J.R.R. Tolkien', 19.99, 'Fantasy'),
('The Da Vinci Code', 'Dan Brown', 22.99, 'Mystery'),
('The Lord of the Rings', 'J.R.R. Tolkien', 39.99, 'Fantasy'),
('Harry Potter and the Sorcerer\'s Stone', 'J.K. Rowling',
29.99, 'Fantasy'),
('The Secret Garden', 'Frances Hodgson Burnett', 7.99,
'Children'),
('Fahrenheit 451', 'Ray Bradbury', 14.99, 'Dystopian'),
('The Odyssey', 'Homer', 12.99, 'Classic'),
('Sherlock Holmes', 'Arthur Conan Doyle', 18.99, 'Mystery'),
('The Count of Monte Cristo', 'Alexandre Dumas', 21.99,
'Adventure'),
('The Chronicles of Narnia', 'C.S. Lewis', 17.99, 'Fantasy');

-- Insert 25 customers into the 'customer' table


INSERT INTO customer (Cname, Bname, Price, Category)
VALUES
('Alice', 'Python for Beginners', 29.99, 'Programming'),
('Bob', 'The Great Adventure', 19.99, 'Novel'),
('Charlie', 'Introduction to Algorithms', 59.99, 'Study Guide'),
('David', 'The Mystery of the Haunted House', 12.99, 'Story'),
('Eva', 'Learn SQL Quickly', 24.99, 'Programming'),
('Frank', 'Machine Learning Basics', 49.99, 'Technology'),
('Grace', 'The Art of War', 9.99, 'History'),
('Helen', 'Moby Dick', 14.99, 'Classic'),
('Igor', 'The Great Gatsby', 11.99, 'Classic'),
('Jack', '1984', 15.99, 'Dystopian'),
('Karen', 'To Kill a Mockingbird', 13.99, 'Classic'),
('Liam', 'Pride and Prejudice', 16.99, 'Romance'),
('Mona', 'War and Peace', 25.99, 'Historical Fiction'),
('Nina', 'The Catcher in the Rye', 10.99, 'Fiction'),
('Oscar', 'A Brief History of Time', 29.99, 'Science'),
('Paul', 'The Hobbit', 19.99, 'Fantasy'),
('Quincy', 'The Da Vinci Code', 22.99, 'Mystery'),
('Rachel', 'The Lord of the Rings', 39.99, 'Fantasy'),
('Sam', 'Harry Potter and the Sorcerer\'s Stone', 29.99,
'Fantasy'),
('Tina', 'The Secret Garden', 7.99, 'Children'),
('Ursula', 'Fahrenheit 451', 14.99, 'Dystopian'),
('Victor', 'The Odyssey', 12.99, 'Classic'),
('Wendy', 'Sherlock Holmes', 18.99, 'Mystery'),
('Xander', 'The Count of Monte Cristo', 21.99, 'Adventure'),
('Yara', 'The Chronicles of Narnia', 17.99, 'Fantasy');
OUTPUT:
1.When adding a book (Option 1):

The options are:


1. Add book
2. Search book
3. Remove book
4. Add customer
5. Search customer
6. Remove customer
7. Exit
Enter the option (1,2,3,4,5,6,7): 1
Enter the book name: Python for Beginners
Enter the author name: John Doe
Enter the price of the book: 29.99
Enter the category of the book (Comic, Story, Novel, Study Guide,
Subject): Programming
Book added successfully.

2. When searching for a book by category (Option 2):

The options are:


1. Add book
2. Search book
3. Remove book
4. Add customer
5. Search customer
6. Remove customer
7. Exit

Enter the option (1,2,3,4,5,6,7): 2


Enter the category (Bookname, Novel, Guide, Story, Subject, Comic):
Programming
('Python for Beginners', 'John Doe', 29.99, 'Programming')

3. When removing a book (Option 3):


markdown

The options are:


1. Add book
2. Search book
3. Remove book
4. Add customer
5. Search customer
6. Remove customer
7. Exit

Enter the option (1,2,3,4,5,6,7): 3


Enter the book name to remove: Python for Beginners
Book deleted.

4. When adding a customer (Option 4):

The options are:


1. Add book
2. Search book
3. Remove book
4. Add customer
5. Search customer
6. Remove customer
7. Exit

Enter the option (1,2,3,4,5,6,7): 4


Enter the Customer name: Alice
Enter the book name: Python for Beginners
Enter the price of the book: 29.99
Enter the category of the book (Comic, Story, Novel, Study Guide,
Subject): Programming
Customer added successfully.

5. When searching for a customer (Option 5):

The options are:


1. Add book
2. Search book
3. Remove book
4. Add customer
5. Search customer
6. Remove customer
7. Exit

Enter the option (1,2,3,4,5,6,7): 5


Enter Customer name to search: Alice
('Alice', 'Python for Beginners', 29.99, 'Programming')

6. When removing a customer (Option 6):

The options are:


1. Add book
2. Search book
3. Remove book
4. Add customer
5. Search customer
6. Remove customer
7. Exit
Enter the option (1,2,3,4,5,6,7): 6
Enter the customer name to delete: Alice
Customer deleted.

7. Exiting the program (Option 7):

The options are:


1. Add book
2. Search book
3. Remove book
4. Add customer
5. Search customer
6. Remove customer
7. Exit

Enter the option (1,2,3,4,5,6,7): 7


Thank you
Sample Invalid Inputs:
Invalid Option:

Enter the option (1,2,3,4,5,6,7): 8


Invalid option. Please choose a number between 1 and 7.
Invalid Price Input (when adding a book or customer):
Enter the price of the book: abc
Invalid price. Please enter a valid number.

You might also like