INFORMATICS
PRACTICES
PROJECT FILE
TOPIC:-
LIBRARY MANAGEMENT
SYSTEM
NAME- SHAURYA SINGH
CLASS-XII-B
Roll no:-25
LIBRARY MANAGEMENT SYSTEM
SOURCE CODE
import [Link]
def connect_db():
return [Link](
host="localhost",
user="root",
2 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
password="183228",
database="Library"
def view_books():
con = connect_db()
cur = [Link]()
[Link]("SELECT * FROM library")
result = [Link]()
print("\nAll Books in Library:\n")
for row in result:
print(row)
[Link]()
def add_book():
con = connect_db()
cur = [Link]()
cid = int(input("Enter Customer ID: "))
name = input("Enter Customer Name: ")
bid = int(input("Enter Book ID: "))
title = input("Enter Book Title: ")
genre = input("Enter Genre: ")
price = float(input("Enter Price: "))
query = "INSERT INTO library VALUES (%s, %s, %s, %s, %s, %s)"
data = (cid, name, bid, title, genre, price)
3 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
[Link](query, data)
[Link]()
print("Book added successfully.")
[Link]()
def search_book():
con = connect_db()
cur = [Link]()
title = input("Enter Book Title to Search: ")
query = "SELECT * FROM library WHERE BookTitle LIKE %s"
[Link](query, ('%' + title + '%',))
result = [Link]()
print("\nSearch Results:\n")
if result:
for row in result:
print(row)
else:
print("No matching book found.")
[Link]()
def update_price():
con = connect_db()
cur = [Link]()
4 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
bid = int(input("Enter Book ID to Update Price: "))
new_price = float(input("Enter New Price: "))
query = "UPDATE library SET Price=%s WHERE BookID=%s"
[Link](query, (new_price, bid))
[Link]()
print("Price updated successfully.")
[Link]()
def delete_book():
con = connect_db()
cur = [Link]()
bid = int(input("Enter Book ID to Delete: "))
query = "DELETE FROM library WHERE BookID=%s"
[Link](query, (bid,))
[Link]()
print("Book deleted successfully.")
[Link]()
def sort_books():
con = connect_db()
cur = [Link]()
[Link]("SELECT * FROM library ORDER BY Price")
5 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
result = [Link]()
print("\nBooks Sorted by Price:\n")
for row in result:
print(row)
[Link]()
def count_books():
con = connect_db()
cur = [Link]()
[Link]("SELECT COUNT(*) FROM library")
count = [Link]()[0]
print("\nTotal Books in Library:", count)
[Link]()
def show_by_genre():
con = connect_db()
cur = [Link]()
genre = input("Enter Genre: ")
query = "SELECT * FROM library WHERE Genre=%s"
[Link](query, (genre,))
result = [Link]()
6 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
print("\nBooks in Genre:", genre, "\n")
if result:
for row in result:
print(row)
else:
print("No books found in this genre.")
[Link]()
def main():
while True:
print("\nLibrary Management System")
print("1. View All Books")
print("2. Add New Book")
print("3. Search Book by Title")
print("4. Update Book Price")
print("5. Delete Book")
print("6. Sort Books by Price")
print("7. Count Total Books")
print("8. Show Books by Genre")
print("9. Exit")
choice = int(input("Enter choice: "))
if choice == 1:
7 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
view_books()
elif choice == 2:
add_book()
elif choice == 3:
search_book()
elif choice == 4:
update_price()
elif choice == 5:
delete_book()
elif choice == 6:
sort_books()
elif choice == 7:
count_books()
elif choice == 8:
show_by_genre()
elif choice == 9:
print("Program ended.")
break
else:
print("Invalid choice. Try again.")
main()
8 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
9 | Page
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
OUTPUT
10 | P a g e
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
11 | P a g e
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
12 | P a g e
INFORMATIC PRACTICES
LIBRARY MANAGEMENT SYSTEM
13 | P a g e
INFORMATIC PRACTICES