Hone logo
Hone
Problems

Simple Library Management System with SQLAlchemy

This challenge focuses on implementing basic database interaction using SQLAlchemy in Python. You'll be building a simplified library management system, allowing you to define a Book model, create a database, add books, query for books, and update their availability. This exercise will solidify your understanding of SQLAlchemy's core concepts like defining models, connecting to a database, and performing CRUD (Create, Read, Update, Delete) operations.

Problem Description

You are tasked with creating a simple library management system using SQLAlchemy. The system should allow you to:

  1. Define a Book model: This model should have the following attributes:

    • id: Integer, primary key, autoincrementing.
    • title: String, not null.
    • author: String, not null.
    • available: Boolean, indicating whether the book is currently available (default: True).
  2. Create a database: Use SQLite for simplicity. The database file should be named library.db.

  3. Add books to the database: Implement a function to add new books to the database, taking the title and author as input.

  4. Query for books: Implement a function to retrieve all books from the database.

  5. Update book availability: Implement a function to mark a book as unavailable (set available to False) given its title.

  6. Handle potential errors: Gracefully handle cases where a book with the given title doesn't exist when attempting to update its availability.

Examples

Example 1:

Input:
- Database: library.db (created and populated with some books)
- Function call: add_book("The Lord of the Rings", "J.R.R. Tolkien")
Output:
- A new record is added to the database with title "The Lord of the Rings", author "J.R.R. Tolkien", and available = True.
Explanation: The `add_book` function creates a new `Book` object, adds it to the database session, and commits the changes.

Example 2:

Input:
- Database: library.db (populated with books)
- Function call: get_all_books()
Output:
- A list of `Book` objects representing all books in the database.
Explanation: The `get_all_books` function queries the database for all `Book` objects and returns them as a list.

Example 3:

Input:
- Database: library.db (populated with books)
- Function call: mark_book_unavailable("Pride and Prejudice")
Output:
- The `available` attribute of the book "Pride and Prejudice" is updated to False in the database.
Explanation: The `mark_book_unavailable` function queries the database for the book with the given title, updates its `available` attribute, and commits the changes. If the book doesn't exist, a message is printed to the console.

Constraints

  • The database should be an SQLite database named library.db.
  • All functions should be well-documented with docstrings.
  • Error handling should be implemented to prevent the program from crashing if a book is not found.
  • The code should be modular and easy to understand.
  • You are expected to use SQLAlchemy's ORM features.

Notes

  • Remember to create the necessary tables in the database. SQLAlchemy will handle this automatically if the tables don't exist.
  • Consider using a session to interact with the database.
  • Think about how to handle potential errors, such as trying to update the availability of a book that doesn't exist.
  • Focus on demonstrating the core concepts of SQLAlchemy: defining models, connecting to a database, and performing CRUD operations. You don't need to implement a full-fledged user interface or complex features.
  • The available column should default to True when a new book is added.
Loading editor...
python