May-07-2022, 03:25 PM
Hello,
I made an inventory system using SQLite and Python. I'm working on a 'checkoutInventory()' function that let's a user add items to a cart and automatically subtracts the items current quantity from the quantity that the user is taking out.
My Question is how do I program a statement that subtracts the user's value from the Quantity column in the SQLite database?
Thanks in advance?
My checkoutInventory() Function right now:
I made an inventory system using SQLite and Python. I'm working on a 'checkoutInventory()' function that let's a user add items to a cart and automatically subtracts the items current quantity from the quantity that the user is taking out.
My Question is how do I program a statement that subtracts the user's value from the Quantity column in the SQLite database?
Thanks in advance?
My checkoutInventory() Function right now:
#---------------------------------------------------------------
# Checkout Inventory Items
#---------------------------------------------------------------
def checkoutInventory():
#Connect to the inventory database (inventory.db)
connection = sqlite3.connect("inventory.db")
cursor = connection.cursor()
print('=============================')
print('= Checkout From Inventory =')
print('=============================')
print("Find an item to checkout by entering it's ID or Name")
print('(1) Checkout Item by ID')
print('(2) Checkout Item by Name')
print('(3) View Cart')
CHOICE = input("Enter choice: ")
#----- Update Item by ID -----
#Choose Item to update by ID
if CHOICE == '1':
userQueryID = input('Item ID: ')
#->Show info for the currently selected item
cursor.execute('SELECT * FROM items WHERE ID = ?', (userQueryID,))
result = cursor.fetchall()
#Print Results/Info in rows
print('\n--------------------')
print('Current Info For Item: ' + userQueryID)
print('--------------------')
for row in result:
print('\n--------------------')
print("Item ID: ", row[0])
print("Item Name: ", row[1])
print("Item Quantity: ", row[2])
print("Item Price: $", row[3])
print("Item Sell Price: $", row[4])
print("Item Description: ", row[5])
print("Item Category: ", row[6])
print("Item Location: ", row[7])
print("Last Updated: ", row[10])
print('--------------------\n')
#-->Bring up checkout menu
print('------------------------------')
print("Would you like to add this item to cart?")
print('(y) Yes')
print('(n) No')
CHOICE = input("Enter choice: ")
#Yes
if CHOICE == 'y' or CHOICE == 'Y':
#Ask for quantity
userQueryQuantity = int(input('How much quantity are you taking: '))
#Remove the quantity from the inventory
#TODO: Update this Update statement so it subtracts user's value from current quantity:
cursor.execute("UPDATE items SET Quantity = ? WHERE ID = ?" , (userQueryQuantity, userQueryID,))
connection.commit()
#Add item to cart
#TODO: Make somthing that stores the currently selected item (maybe a list?)
#That way the user can add more stuff to the cart or actually checkout(which will clear the cart
# since the quantity values have alreday beeen subtracted)
print('The item with the ID: ' + userQueryID + 'has been added to cart')
#No
elif CHOICE == 'n' or CHOICE == 'N':
#Return to Checkout Menu
checkoutInventory()
