Yugank library code
Yugank library code
import mysql.connector
import pyfiglet
import requests
import wikipediaapi
from datetime import datetime, timedelta
print("-----------------------")
print("About Library")
print("-----------------------")
# Display library information
print("Year of Library's Establishment :", 2023)
print("Name of the Librarian :", userName[0][0])
print("Total Number of Books Available in the Library :", len(totalBooks))
print("Total Number of Users Enrolled in the Library :", len(totalUsers))
print("-----------------------")
userMenu()
try:
c.execute(
"INSERT INTO users (userName, userEmail, userPhone, userAddress) VALUES
(%s, %s, %s, %s)",
(userName, userEmail, userPhone, userAddress),
)
db.commit()
print("User added successfully!")
except Exception as e:
print(f"Error: {e}")
print("-----------------------")
userMenu()
try:
c.execute(
"INSERT INTO books (bookName, bookAuthor, bookYear, bookGenre) VALUES
(%s, %s, %s, %s)",
(bookName, bookAuthor, bookYear, bookGenre),
)
db.commit()
print("Book added successfully!")
except Exception as e:
print(f"Error: {e}")
print("-----------------------")
userMenu()
try:
# Check if the book is available
c.execute("SELECT issueStatus FROM books WHERE bookId=%s", (bookId,))
book = c.fetchone()
if book and book[0] == "Available":
c.execute(
"INSERT INTO issuedBooks (userId, bookId, issueDate) VALUES (%s,
%s, %s)",
(userId, bookId, datetime.now()),
)
c.execute(
"UPDATE books SET issueStatus='Issued' WHERE bookId=%s", (bookId,)
)
db.commit()
print("Book issued successfully!")
else:
print("Book is not available for issuing.")
except Exception as e:
print(f"Error: {e}")
print("-----------------------")
userMenu()
try:
# Retrieve issue details
c.execute(
"SELECT bookId, issueDate FROM issuedBooks WHERE issueId=%s",
(issueId,)
)
issue = c.fetchone()
if issue:
bookId, issueDate = issue
returnDate = datetime.now()
daysIssued = (returnDate - issueDate).days
# Update records
c.execute(
"DELETE FROM issuedBooks WHERE issueId=%s", (issueId,)
)
c.execute(
"UPDATE books SET issueStatus='Available' WHERE bookId=%s",
(bookId,)
)
db.commit()
try:
c.execute("SELECT * FROM books WHERE bookGenre LIKE %s", ("%" + genre +
"%",))
results = c.fetchall()
if results:
for row in results:
print(f"Book ID: {row[0]}, Name: {row[1]}, Author: {row[7]}")
print("-----------------------")
else:
print("No books found for the given genre.")
except Exception as e:
print(f"Error: {e}")
print("-----------------------")
userMenu()
try:
# Retrieve genres of previously borrowed books
c.execute(
"SELECT DISTINCT bookGenre FROM books WHERE bookId IN (SELECT bookId
FROM issuedBooks WHERE userId = %s)",
(userId,),
)
genres = c.fetchall()
if genres:
for genre in genres:
c.execute("SELECT * FROM books WHERE bookGenre = %s LIMIT 3",
(genre[0],))
recommendations = c.fetchall()
try:
c.execute(
"SELECT b.bookName, b.bookAuthor, i.issueDate FROM books b JOIN
issuedBooks i ON b.bookId = i.bookId WHERE i.userId = %s",
(userId,),
)
borrowedBooks = c.fetchall()
if borrowedBooks:
for book in borrowedBooks:
print(f"Book Name: {book[0]}, Author: {book[1]}, Issued On:
{book[2]}")
print("-----------------------")
else:
print("No borrowed books found.")
except Exception as e:
print(f"Error: {e}")
print("-----------------------")
userMenu()
# Main Function
def main():
print(pyfiglet.figlet_format("Library Management System"))
userMenu()