0% found this document useful (0 votes)
29 views15 pages

Ooooooooo

This program manages a library using a MySQL database. It allows users to add, borrow, return and view books in the library. It contains functions to perform each action and connects to a MySQL database called "library_management" to store and retrieve book data from the "books" table. The main menu displays options to perform each action and takes user input to call the corresponding function to interface with the MySQL database and library.

Uploaded by

sreeragmarch2006
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)
29 views15 pages

Ooooooooo

This program manages a library using a MySQL database. It allows users to add, borrow, return and view books in the library. It contains functions to perform each action and connects to a MySQL database called "library_management" to store and retrieve book data from the "books" table. The main menu displays options to perform each action and takes user input to call the corresponding function to interface with the MySQL database and library.

Uploaded by

sreeragmarch2006
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/ 15

DATA DIRECTORY

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

# Connect to MySQL without specifying the database


db = mysql.connector.connect(
host="localhost",
user="root",
password="root"
)

# Create a cursor object to interact with the database


cursor = db.cursor()

# Create the 'library_management' database


cursor.execute("CREATE DATABASE IF NOT EXISTS
library_management")
# Switch to the 'library_management' database
cursor.execute("USE library_management")

# Drop the 'books' table if it exists


cursor.execute("DROP TABLE IF EXISTS books")

# Create a 'books' table


cursor.execute("CREATE TABLE books (id INT
AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255),
author VARCHAR(255), status VARCHAR(50) DEFAULT
'available')")

# Insert 5 predefined books into the 'books' table


using individual execute statements
predefined_books = [
("The Catcher in the Rye", "J.D. Salinger"),
("To Kill a Mockingbird", "Harper Lee"),
("1984", "George Orwell"),
("Pride and Prejudice", "Jane Austen"),
("The Great Gatsby", "F. Scott Fitzgerald")
]

insert_query = "INSERT INTO books (title, author)


VALUES (%s, %s)"

for book in predefined_books:


cursor.execute(insert_query, book)

# Commit the changes


db.commit()

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")

choice = input("Enter your choice (1-5): ")

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

You might also like