Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Part 2:

To create a Python script that connects to the database created and performs the required tasks, we can
use the SQLite3 module in Python. Here's an example code snippet:

import sqlite3

# Connect to the database

conn = sqlite3.connect('books.db')

# Create a cursor object

c = conn.cursor()

# Ask the user for input

title = input("Enter the title of the book: ")

quantity = int(input("Enter the quantity purchased: "))

# Select the price from the books table

c.execute("SELECT price FROM books WHERE title=?", (title,))

result = c.fetchone()

# Calculate the total amount

if result is not None:

price = result[0]

total = price * quantity

print("The total amount for {} copies of {} is ${:.2f}".format(quantity, title, total))

else:

print("Book not found in database.")

# Close the connection

conn.close()

You might also like