CLASS XII (CBSE) COMPUTER SCIENCE PROJECT
CLASS XII (CBSE) COMPUTER SCIENCE PROJECT
PROJECT
2024-25
ACKNOWLEDGEMENT
I would like to convey my heartfelt gratitude to my
computer Teacher, Mrs Divya M for her continuous
support and help in the completion of my project.
Users can:
connection = mysql.connector.connect(
host='localhost',
user='root',
password='123456',
database='LibraryDB'
)
cursor = connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS BookLibs (
book_id INT PRIMARY KEY AUTO_INCREMENT,
book_title VARCHAR(255),
author VARCHAR(255),
section VARCHAR(100),
age_rating VARCHAR(10),
availability VARCHAR(50)
)
''')
admin_username = "admin"
admin_password = "password"
if not results:
cursor.execute('''
INSERT INTO BookLibs (book_title, author, section, age_rating,
availability)
VALUES
('To Kill a Mockingbird', 'Harper Lee', 'Fiction', '13+',
'Available'),
('1984', 'George Orwell', 'Fiction', '16+', 'Available'),
('Moby Dick', 'Herman Melville', 'Fiction', '13+', 'Available'),
('The Great Gatsby', 'F. Scott Fitzgerald', 'Fiction', '16+',
'Available'),
('War and Peace', 'Leo Tolstoy', 'Historical', '18+', 'Available'),
('Pride and Prejudice', 'Jane Austen', 'Romance', '13+', 'Available'),
('Brave New World', 'Aldous Huxley', 'Science Fiction', '16+',
'Available'),
('The Hobbit', 'J.R.R. Tolkien', 'Fantasy', '12+', 'Available'),
('Fahrenheit 451', 'Ray Bradbury', 'Science Fiction', '14+',
'Available'),
('The Divine Comedy', 'Dante Alighieri', 'Classics', '18+',
'Available')
''')
connection.commit()
def display_books(cursor):
cursor.execute('SELECT * FROM BookLibs')
results = cursor.fetchall()
print("ID | Title | Author | Section | Age Rating | Availability")
for each in results:
print(each[0], each[1], each[2], each[3], each[4], each[5], sep="
| ")
def display_books_by_section(cursor):
message = "Enter the section you want to "
message += "view (e.g., Fiction, Romance, Science Fiction): "
section = input(message).title()
cursor.execute('SELECT * FROM BookLibs WHERE section = %s',
(section,))
results = cursor.fetchall()
if results:
print(f"Books in the '{section}' section:")
print("ID | Title | Author | Section | Age Rating | Availability")
for each in results:
print(each[0],each[1], each[2], each[3], each[4], each[5],
sep=" | ")
else:
print(f"No books found in the '{section}' section.")
def display_books_by_author(cursor):
author = input("Enter the author name to view their books: ").title()
cursor.execute('SELECT * FROM BookLibs WHERE author = %s', (author,))
results = cursor.fetchall()
if results:
print(f"Books by {author}:")
print("ID | Title | Author | Section | Age Rating | Availability")
for each in results:
print(each[0], each[1],each[2], each[3], each[4], each[5],
sep=" | ")
else:
print(f"No books found by '{author}'.")
def display_books_by_genre_and_age(cursor):
message = "Enter the genre you want to view"
message += " (e.g., Fiction, Romance, Science Fiction): "
genre = input(message).title()
message = "Enter the age rating to filter by (e.g., 12+, 13+, 16+): "
age_rating = input().upper()
query = 'SELECT * FROM BookLibs WHERE section = %s AND age_rating =
%s'
cursor.execute(query, (genre, age_rating))
results = cursor.fetchall()
if results:
print(f"Books in the '{genre}' genre for age '{age_rating}':")
print("ID | Title | Author | Section | Age Rating | Availability")
for each in results:
print(each[0],each[1], each[2], each[3], each[4], each[5],
sep=" | ")
else:
print(f"No books found in the '{genre}' genre for age
'{age_rating}'.")
if choice == 1:
title = input("Enter book title: ")
author = input("Enter book author: ")
section = input("Enter book section: ")
age_rating = input("Enter book age rating (e.g., 12+, 13+): ")
query = 'INSERT INTO BookLibs '
query += '(book_title,author,section,age_rating,availability)'
query += ' VALUES (%s, %s, %s, %s, %s)'
cursor.execute(query,
(title,author,section,age_rating,'Available'))
connection.commit()
message = "\n'{}' by {} has been added to the '{}' section."
print(message.format(title, author, section))
elif choice == 2:
display_books(cursor)
book_id = int(input("Enter the Book ID you want to delete: "))
cursor.execute('DELETE FROM BookLibs WHERE book_id = %s',
(book_id,))
connection.commit()
print(f"\nBook with ID {book_id} has been deleted.")
elif choice == 3:
display_books(cursor)
elif choice == 4:
display_books_by_section(cursor)
elif choice == 5:
display_books_by_author(cursor)
elif choice == 6:
display_books_by_genre_and_age(cursor)
elif choice == 7:
print("Logging out...")
time.sleep(2)
return
else:
print("Invalid Choice! Please try again.")
if choice == 1:
rent_book(cursor, connection)
elif choice == 2:
return_book(cursor, connection)
elif choice == 3:
display_books_by_section(cursor)
elif choice == 4:
display_books_by_author(cursor)
elif choice == 5:
display_books_by_genre_and_age(cursor)
elif choice == 6:
connection.close()
print("Goodbye!")
break
else:
print("Invalid Choice! Please try again.")
def login():
username = input("Enter admin username: ")
password = input("Enter admin password: ")
def main():
print("1: Customer Login")
print("2: Admin Login")
print("3: Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
library_system(connection, cursor)
elif choice == 2:
login()
elif choice == 3:
print("Goodbye!")
connection.close()
else:
print("Invalid Choice! Please try again.")
main()
main()
Python Output
Customer
1: Customer Login
2: Admin Login
3: Exit
Enter your choice: 1
Welcome to the Library!
1: Rent a Book
2: Return a Book
3: View Books by Section
4: View Books by Author
5: View Books by Genre and Age Rating
6: Exit
Enter your choice: 1
ID | Title | Author | Section | Age Rating | Availability
1 | To Kill a Mockingbird | Harper Lee | Fiction | 13+ | Available
2 | 1984 | George Orwell | Fiction | 16+ | Available
3 | Moby Dick | Herman Melville | Fiction | 13+ | Available
4 | The Great Gatsby | F. Scott Fitzgerald | Fiction | 16+ | Available
5 | War and Peace | Leo Tolstoy | Historical | 18+ | Available
6 | Pride and Prejudice | Jane Austen | Romance | 13+ | Available
7 | Brave New World | Aldous Huxley | Science Fiction | 16+ | Available
8 | The Hobbit | J.R.R. Tolkien | Fantasy | 12+ | Available
9 | Fahrenheit 451 | Ray Bradbury | Science Fiction | 14+ | Available
10 | The Divine Comedy | Dante Alighieri | Classics | 18+ | Available
11 | a | b | c | 12+ | Available