Advertisement
ksieradzinski

Untitled

Jun 5th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import sqlite3
  2.  
  3. def init_db():
  4. with sqlite3.connect("produkty.db") as connection:
  5. cursor = connection.cursor()
  6. cursor.execute("""
  7. CREATE TABLE IF NOT EXISTS products (
  8. id INTEGER PRIMARY KEY AUTOINCREMENT,
  9. name TEXT NOT NULL,
  10. price REAL NOT NULL
  11. )
  12. """)
  13. connection.commit()
  14.  
  15. def add_product(name: str, price:float):
  16. with sqlite3.connect("produkty.db") as connection:
  17. cursor = connection.cursor()
  18. cursor.execute(
  19. "INSERT INTO products(name, price) VALUES(?, ?)",
  20. (name, price)
  21. )
  22. connection.commit()
  23.  
  24. def get_products():
  25. with sqlite3.connect("produkty.db") as connection:
  26. cursor = connection.cursor()
  27. cursor.execute("SELECT id, name, price FROM products ORDER BY price ASC")
  28.  
  29. for product_id, name, price in cursor.fetchall():
  30. print(f" {product_id}. {name} kosztuje {price} PLN")
  31.  
  32. def get_product_price(product_id:int):
  33. with sqlite3.connect("produkty.db") as connection:
  34. cursor = connection.cursor()
  35. cursor.execute("SELECT price FROM products WHERE id=?", (product_id,))
  36. return cursor.fetchone()
  37.  
  38.  
  39. print("Produkty w ofercie: ")
  40. get_products()
  41.  
  42. action = input("Co chcesz zrobić? k - kupić, d - dodać: ")
  43. if action == "d":
  44. name = input("Nazwa: ")
  45. price = float(input("Cena: "))
  46. add_product(name, price)
  47. print("---" * 10)
  48. get_products()
  49. if action == "k":
  50. product_id = int(input("Podaj numer produktu, który chcesz kupić: "))
  51. price = get_product_price(product_id)[0]
  52. quantity = int(input("Ile sztuk produktu chcesz zakupić? "))
  53. print(f"Świetnie, do zapłaty {price * quantity}. Zaraz przyniosę.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement