Ooooooooo
Ooooooooo
MODULES
Import sys
import mysql.connector
FUNCTIONS()
Add_books()
Borrow_books()
Return_books()
View_books()
SYNOPSIS
This program is used for management
of library.
The program includes four options.
These are:
1 .TO ADD BOOKS
2 .TO BORROW BOOKS
3 .TO RETURN BOOKS
4 .TO VIEW BOOKS
SOURCE CODE
import mysql.connector
import sys
def print_welcome_message():
print("*")
print("* Welcome to Random Library *")
print("* Your gateway to a world of books *")
print("*")
def add_book():
title = input("Enter the title of the book: ")
author = input("Enter the author of the book: ")
# Insert a new book into the 'books' table
query = "INSERT INTO books (title, author) VALUES
(%s, %s)"
values = (title, author)
cursor.execute(query, values)
db.commit()
print("Book added successfully!")
def borrow_book():
book_id = input("Enter the ID of the book you want
to borrow: ")
# Check if the book is available before borrowing
query = "SELECT * FROM books WHERE id = %s AND
status = 'available'"
cursor.execute(query, (book_id,))
book = cursor.fetchone()
if book:
# Update book status to 'checked out'
update_query = "UPDATE books SET status =
'checked out' WHERE id = %s"
cursor.execute(update_query, (book_id,))
db.commit()
print("Book borrowed successfully!")
else:
print("Book not available for borrowing.")
def return_book():
book_id = input("Enter the ID of the book you want
to return: ")
# Update book status to 'available' upon return
update_query = "UPDATE books SET status =
'available' WHERE id = %s"
cursor.execute(update_query, (book_id,))
db.commit()
print("Book returned successfully!")
def view_books():
# Retrieve and display all books from the 'books'
table
query = "SELECT * FROM books"
cursor.execute(query)
books = cursor.fetchall()
if not books:
print("No books available.")
else:
print("ID | Title | Author")
print("-------------------------------------------------------------")
for book in books:
print(f"{book[0]:<4} | {book[1]:<35} |
{book[2]:<20}")
# Interactive menu
while True:
print_welcome_message()
print("\nLibrary Management System Menu:")
print("1. Add a Book")
print("2. Borrow a Book")
print("3. Return a Book")
print("4. View All Books")
print("5. Exit")
if choice == "1":
add_book()
elif choice == "2":
borrow_book()
elif choice == "3":
return_book()
elif choice == "4":
view_books()
elif choice == "5":
sys.exit("Exiting the Library Management
System.")
else:
print("Invalid choice,Please enter a number
between 1 and 5")
output
ADD_BOOK()
BORROW_BOOK()
RETURN_BOOK()
VIEW_BOOKS()
MYSQL
CONCLUSION
By using this code we created the library
management system. Nothing is perfect in
this world. So,we are also no exception.
Although we have tried our best to present
this information effectively,there can be
further enhancements in the application.
We have taken care of all the critical aspects
during the development of this project. Like
all other things, this project also has some
limitations and can further be enhanced by
someone because there are drawbacks that
do not permit the system to be 100%
accurate.
BIBLIOGRAPHY
Computer science with python textbook by Preeti
Arora