import sqlite3
class Book:
"""Represents a book with a title, author, genre, and summary."""
def __init__(self, title, author, genre, summary):
"""Initialize the book with title, author, genre, and summary."""
self.title = title
self.author = author
self.genre = genre
self.summary = summary
def __str__(self):
"""Return a string representation of the book."""
return f"Title: {self.title}\nAuthor: {self.author}\nGenre: {self.genre}\nSummary: {self.summary}\n"
class BookDatabase:
"""Manages a database of books."""
def __init__(self, db_name="books.db"):
"""Initialize the database connection."""
self.db_name = db_name
self.connection = sqlite3.connect(self.db_name)
self.cursor = self.connection.cursor()
def get_all_books(self):
"""Retrieve all books from the database."""
self.cursor.execute("SELECT title, author, genre, summary FROM books")
rows = self.cursor.fetchall()
return [Book(title, author, genre, summary) for title, author, genre, summary in rows]
def close(self):
"""Close the database connection."""
self.connection.close()
def display_books():
"""Display all books stored in the database."""
db = BookDatabase()
books = db.get_all_books()
if not books:
print("No books found in the database.")
else:
print("\nBooks stored in the database:\n")
for book in books:
print(book)
db.close()
# Test Program
if __name__ == "__main__":
display_books()
Output
Books stored in the database:
Title: The Great Gatsby
Author: F. Scott Fitzgerald
Genre: Fiction
Summary: A story about Jay Gatsby's obsession with Daisy Buchanan.
Title: 1984
Author: George Orwell
Genre: Dystopian
Summary: A novel about a totalitarian regime led by Big Brother.