Question: I have the Python code for the task. Create a program that can be used by a bookstore clerk. The program should allow the clerk

I have the Python code for the task.

Create a program that can be used by a bookstore clerk. The program should allow the clerk to:

add new books to the database

update book information delete books from the database

search the database to find a specific book.

Create a database called ebookstore and a table called books. The table should have the following structure:

id

Title

Author

Qty

3001

A Tale of Two Cities

Charles Dickens

30

3002

Harry Potter and the Philosopher's Stone

J.K. Rowling

40

3003

The Lion, the Witch and the Wardrobe

C. S. Lewis

25

3004

The Lord of the Rings

J.R.R Tolkien

37

3005

Alice in Wonderland

Lewis Carroll

12

Populate the table with the above values. You can also add your own values if you wish.

The program should present the user with the following menu:

1. Enter book

2. Update book

3. Delete book

4. Search books

0. Exit

What will be the code for SQLite to add the table data to the database so my program can find the books from that table? My code :

import sqlite3

# Establishing a connection to the SQLite database

db = sqlite3.connect('ebookstore.db')

# Defining a function to retrieve book data from the database

def get_book(id):

cursor = db.cursor()

query = "SELECT * FROM books WHERE id = ?"

cursor.execute(query, (id,))

result = cursor.fetchone()

cursor.close()

return result

# Defining a function to add a new book to the database

def add_book(id, title, author, qty):

cursor = db.cursor()

query = "INSERT INTO books (id, Title, Author, Qty) VALUES (?, ?, ?, ?)"

values = (id, title, author, qty)

cursor.execute(query, values)

db.commit()

cursor.close()

# Defining a function to update the details of an existing book in the database

def update_book(id, title, author):

cursor = db.cursor()

query = "UPDATE books SET Title = ?, Author = ? WHERE id = ?"

values = (title, author, id)

cursor.execute(query, values)

db.commit()

cursor.close()

# Defining a function to remove a book from the database

def remove_book(id):

cursor = db.cursor()

query = "DELETE FROM books WHERE id = ?"

cursor.execute(query, (id,))

db.commit()

cursor.close()

# The main function of the program

def main():

print("Welcome to the eBookstore!")

while True:

print("1. Add book")

print("2. Update book")

print("3. Remove book")

print("4. Search book")

print("5. Exit")

choice = input("Enter your choice: ")

# Add a new book to the database

if choice == "1":

id = input("Enter book id: ")

title = input("Enter book title: ")

author = input("Enter book author: ")

qty = input("Enter book quantity: ")

add_book(id, title, author, qty)

print("Book added to the database")

# Update the details of an existing book in the database

elif choice == "2":

id = input("Enter book id: ")

book = get_book(id)

if book:

title = input("Enter new book title: ")

author = input("Enter new book author: ")

update_book(id, title, author)

print("Book details updated in the database")

else:

print("No book found with that id")

# Remove a book from the database

elif choice == "3":

id = input("Enter book id: ")

book = get_book(id)

if book:

remove_book(id)

print("Book removed from the database")

else:

print("No book found with that id")

# Search for a book in the database

elif choice == "4":

id = input("Enter book id: ")

book = get_book(id)

if book:

print(f"Book found: {book}")

else:

print("No book found with that id")

# Exit the program

elif choice == "5":

print("Goodbye!")

break

# Handle invalid choices

else:

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

if __name__ == "__main__":

main()

THANKS!!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!