0% found this document useful (0 votes)
3 views4 pages

Library Management System

The document outlines a Library Management System implemented in Python using SQLite for database management. It includes functionalities to add, view, remove, update, and search for books, as well as features for borrowing and returning books. The system provides a command-line interface for user interaction and manages book records through a structured database table.

Uploaded by

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

Library Management System

The document outlines a Library Management System implemented in Python using SQLite for database management. It includes functionalities to add, view, remove, update, and search for books, as well as features for borrowing and returning books. The system provides a command-line interface for user interaction and manages book records through a structured database table.

Uploaded by

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

LIBRARY MANAGEMENT SYSTEM

import sqlite3

connection = sqlite3.connect('library.db')
cursor = connection.cursor()

# Create the books table


cursor.execute('''
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
author TEXT NOT NULL,
isbn TEXT NOT NULL UNIQUE
)
''')

def add_book(title, author, isbn):


try:
cursor.execute('INSERT INTO books (title, author, isbn) VALUES (?, ?, ?)', (title, author,
isbn))
connection.commit()
print(f'Book "{title}" added successfully.')
except sqlite3.IntegrityError:
print("Error: Book with this ISBN already exists.")
except Exception as e:
print(f"An error occurred: {e}")

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 update_book(isbn, new_title, new_author):


cursor.execute('UPDATE books SET title = ?, author = ? WHERE isbn = ?', (new_title,
new_author, isbn))
if cursor.rowcount:
connection.commit()
print("Book updated 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.")

elif choice == '2':


view_books()
elif choice == '3':
isbn = input("Enter book ISBN to remove: ")
remove_book(isbn)

elif choice == '4':


isbn = input("Enter book ISBN to update: ")
new_title = input("Enter new title: ")
new_author = input("Enter new author: ")
if new_title and new_author:
update_book(isbn, new_title, new_author)
else:
print("Error: Title and Author cannot be empty.")

elif choice == '5':


search_term = input("Enter title or author to search: ")
search_books(search_term)

elif choice == '6':


print("Exiting the system.")
break

else:
print("Invalid choice. Please try again.")

try:
main()
finally:
connection.close()

Features Added

1. Search Books: Users can search for books by title or author.


2. Update Book Information: Users can update the title, author, or ISBN of a book by its
ID.
3. Delete Books: Users can delete a book from the library using its ID.
4. Borrow and Return Books: Users can borrow a book by its ISBN, and mark it as
returned when done.

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

You might also like