Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- def init_db():
- with sqlite3.connect("produkty.db") as connection:
- cursor = connection.cursor()
- cursor.execute("""
- CREATE TABLE IF NOT EXISTS products (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT NOT NULL,
- price REAL NOT NULL
- )
- """)
- connection.commit()
- def add_product(name: str, price:float):
- with sqlite3.connect("produkty.db") as connection:
- cursor = connection.cursor()
- cursor.execute(
- "INSERT INTO products(name, price) VALUES(?, ?)",
- (name, price)
- )
- connection.commit()
- def get_products():
- with sqlite3.connect("produkty.db") as connection:
- cursor = connection.cursor()
- cursor.execute("SELECT id, name, price FROM products ORDER BY price ASC")
- for product_id, name, price in cursor.fetchall():
- print(f" {product_id}. {name} kosztuje {price} PLN")
- def get_product_price(product_id:int):
- with sqlite3.connect("produkty.db") as connection:
- cursor = connection.cursor()
- cursor.execute("SELECT price FROM products WHERE id=?", (product_id,))
- return cursor.fetchone()
- print("Produkty w ofercie: ")
- get_products()
- action = input("Co chcesz zrobić? k - kupić, d - dodać: ")
- if action == "d":
- name = input("Nazwa: ")
- price = float(input("Cena: "))
- add_product(name, price)
- print("---" * 10)
- get_products()
- if action == "k":
- product_id = int(input("Podaj numer produktu, który chcesz kupić: "))
- price = get_product_price(product_id)[0]
- quantity = int(input("Ile sztuk produktu chcesz zakupić? "))
- print(f"Świetnie, do zapłaty {price * quantity}. Zaraz przyniosę.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement