Python Forum
Help with subtracting values using SQLite & Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with subtracting values using SQLite & Python
#1
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:
#---------------------------------------------------------------
#                 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()
Reply


Messages In This Thread
Help with subtracting values using SQLite & Python - by Extra - May-07-2022, 03:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  need help with data analysing with python and sqlite Hardcool 2 404 Jan-30-2024, 06:49 AM
Last Post: Athi
  python sqlite autoincrement in primary column janeik 6 1,225 Aug-13-2023, 11:22 AM
Last Post: janeik
  [Solved]Help with search statement-SQLite & Python Extra 1 1,074 May-06-2022, 07:38 PM
Last Post: Extra
  Help With Python SQLite Error Extra 10 15,297 May-04-2022, 11:42 PM
Last Post: Extra
  Python Sqlite georgebijum 0 1,076 May-04-2022, 10:12 AM
Last Post: georgebijum
  Subtracting datetimes [index] Mark17 2 2,516 Aug-21-2021, 12:11 AM
Last Post: Larz60+
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,209 Jun-29-2020, 08:51 PM
Last Post: buran
  Inserting values from multiple lists sqlite azulu 1 2,525 May-24-2020, 08:40 AM
Last Post: ibreeden
  how to use items combobox in table name sqlite in python hampython 1 2,717 May-24-2020, 02:17 AM
Last Post: Larz60+
  Indexing problem while iterating list and subtracting lbtdne 2 2,158 May-14-2020, 10:19 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020