Exercise 11-01:

Make a program that creates the prices.db database with the price table to store a list of sales prices for products. The table must contain the name of the product and its respective price. The program must also insert some data for testing.

Answer:

##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 11/exercise-11-01.py.py
##############################################################################
import sqlite3
from contextlib import closing

with sqlite3.connect("prices.db") as connection:
    with closing(connection.cursor()) as cursor:
        cursor.execute(
            """
                create table prices(
                    name text,
                    price numeric)
                """
        )
        cursor.execute(
            """
                insert into prices (name, price)
                    values(?, ?)
                    """,
            ("Potato", "3.20"),
        )
        cursor.execute(
            """
                insert into prices (name, price)
                    values(?, ?)
                    """,
            ("Bread", "1.20"),
        )
        cursor.execute(
            """
                insert into prices (name, price)
                    values(?, ?)
                    """,
            ("Papaya", "2.14"),
        )
Click here to download the file