-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (48 loc) · 1.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sqlite3
# Create a connection to the SQLite database
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
# Create the 'products' table
cursor.execute('''
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
quantity INTEGER NOT NULL,
price REAL NOT NULL
)
''')
conn.commit()
def add_product(name, quantity, price):
cursor.execute('INSERT INTO products (name, quantity, price) VALUES (?, ?, ?)', (name, quantity, price))
conn.commit()
print(f"{name} added to the inventory.")
def display_inventory():
cursor.execute('SELECT * FROM products')
products = cursor.fetchall()
if not products:
print("Inventory is empty.")
else:
print("Inventory:")
for product in products:
print(f"ID: {product[0]}, Name: {product[1]}, Quantity: {product[2]}, Price: {product[3]}")
def main():
while True:
print("\nOptions:")
print("1. Add Product")
print("2. Display Inventory")
print("3. Exit")
choice = input("Enter your choice (1/2/3): ")
if choice == '1':
name = input("Enter product name: ")
quantity = int(input("Enter quantity: "))
price = float(input("Enter price: "))
add_product(name, quantity, price)
elif choice == '2':
display_inventory()
elif choice == '3':
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()