Library Management System
Library Management System
import sqlite3
connection = sqlite3.connect('library.db')
cursor = connection.cursor()
def view_books():
cursor.execute('SELECT * FROM books')
rows = cursor.fetchall()
if rows:
print("Books in the library:")
for row in rows:
print(f'ID: {row[0]}, Title: {row[1]}, Author: {row[2]}, ISBN: {row[3]}')
else:
print("No books in the library.")
def remove_book(isbn):
cursor.execute('DELETE FROM books WHERE isbn = ?', (isbn,))
if cursor.rowcount:
connection.commit()
print("Book removed successfully.")
else:
print("Book not found.")
def search_books(search_term):
cursor.execute('SELECT * FROM books WHERE title LIKE ? OR author LIKE ?',
(f'%{search_term}%', f'%{search_term}%'))
results = cursor.fetchall()
if results:
for row in results:
print(f'ID: {row[0]}, Title: {row[1]}, Author: {row[2]}, ISBN: {row[3]}')
else:
print("No matching books found.")
def main():
while True:
print("\nLibrary Management System")
print("1. Add Book")
print("2. View Books")
print("3. Remove Book")
print("4. Update Book")
print("5. Search Books")
print("6. Exit")
choice = input("Enter your choice: ")
if choice == '1':
title = input("Enter book title: ")
author = input("Enter book author: ")
isbn = input("Enter book ISBN: ")
if title and author and isbn:
add_book(title, author, isbn)
else:
print("Error: All fields must be filled.")
else:
print("Invalid choice. Please try again.")
try:
main()
finally:
connection.close()
Features Added
Usage
● Add Book: Add a new book with title, author, and ISBN.
● Display Books: View all the books in the library.
● Search Books: Find books by title or author.
● Update Book: Modify details of an existing book.
● Delete Book: Remove a book from the library.
● Borrow Book: Mark a book as borrowed (make it unavailable).
● Return Book: Mark a book as returned (make it available).