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

Shreya

This document contains a Python script for a Library Management System that connects to a MySQL database. It includes functions to add books, view available books, issue books to users, and return issued books. The script features a main menu for user interaction and handles database operations such as inserting, updating, and deleting records.

Uploaded by

skvjict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Shreya

This document contains a Python script for a Library Management System that connects to a MySQL database. It includes functions to add books, view available books, issue books to users, and return issued books. The script features a main menu for user interaction and handles database operations such as inserting, updating, and deleting records.

Uploaded by

skvjict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SOURCE CODE:

import mysql.connector

from datetime import datetime

# Database connection

def connect_db():

return mysql.connector.connect(

host="localhost",

user="root", # Replace with your MySQL username

password="123456", # Replace with your MySQL password

database="LibraryDB"

# Add a new book

def add_book(title, author, quantity):

db = connect_db()

cursor = db.cursor()

query = "INSERT INTO Books (Title, Author, Quantity) VALUES (%s, %s, %s)"

cursor.execute(query, (title, author, quantity))

db.commit()

print("Book added successfully!")

db.close()

# View all books

def view_books():

db = connect_db()

cursor = db.cursor()

cursor.execute("SELECT * FROM Books")

books = cursor.fetchall()
print("\nAvailable Books:")

for book in books:

print(f"ID: {book[0]}, Title: {book[1]}, Author: {book[2]}, Quantity: {book[3]}")

db.close()

# Issue a book

def issue_book(book_id, user_name):

db = connect_db()

cursor = db.cursor()

# Check if the book is available

cursor.execute("SELECT Quantity FROM Books WHERE BookID = %s", (book_id,))

result = cursor.fetchone()

if result and result[0] > 0:

query = "INSERT INTO IssuedBooks (BookID, UserName, IssueDate) VALUES (%s, %s, %s)"

cursor.execute(query, (book_id, user_name, datetime.now().date()))

# Update book quantity

update_query = "UPDATE Books SET Quantity = Quantity - 1 WHERE BookID = %s"

cursor.execute(update_query, (book_id,))

db.commit()

print("Book issued successfully!")

else:

print("Book not available!")

db.close()

# Return a book
def return_book(issue_id):

db = connect_db()

cursor = db.cursor()

# Get the BookID from the issued record

cursor.execute("SELECT BookID FROM IssuedBooks WHERE IssueID = %s", (issue_id,))

result = cursor.fetchone()

if result:

book_id = result[0]

# Delete the issued record

delete_query = "DELETE FROM IssuedBooks WHERE IssueID = %s"

cursor.execute(delete_query, (issue_id,))

# Update book quantity

update_query = "UPDATE Books SET Quantity = Quantity + 1 WHERE BookID = %s"

cursor.execute(update_query, (book_id,))

db.commit()

print("Book returned successfully!")

else:

print("Invalid Issue ID!")

db.close()

# Main menu

def main():

while True:

print("\nLibrary Management System")


print("1. Add Book")

print("2. View Books")

print("3. Issue Book")

print("4. Return Book")

print("5. Exit")

choice = input("Enter your choice: ")

if choice == '1':

title = input("Enter book title: ")

author = input("Enter author name: ")

quantity = int(input("Enter quantity: "))

add_book(title, author, quantity)

elif choice == '2':

view_books()

elif choice == '3':

book_id = int(input("Enter book ID to issue: "))

user_name = input("Enter your name: ")

issue_book(book_id, user_name)

elif choice == '4':

issue_id = int(input("Enter issue ID to return: "))

return_book(issue_id)

elif choice == '5':

print("Exiting...")

break

else:

print("Invalid choice. Try again!")

if __name__ == "__main__":
main()

You might also like