Python Forum

Full Version: Help with Dictionary Database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

So I'm working on an inventory system for myself to keep track of the items I have.
I made a template for a database where all the info for the items will be stored (It's formatted to be a dictionary).

My questions is: will this work for an inventory system? What I mean is will I be able to search for items by name, add new items (keeping the same format: name, price, category, quantity, description, and other field I might want to add in later), view/modify the info, and sort items by category.


If this is possible, how would I make it so I can modify this database and link it to my main script?
(Right now I have a menu for my inventory system that askes if I want to add an item, remove an item, and update an item)

My Dict Database:
# A dictionary to store data
items = {1001: {"name": "MG996R Servo", 
                            "price": 3,
                            "category": "Motors",
                            "quantity": 10,
                            "Torque (kg/cm)" : 11,
                            "Description" : "A standard servo...",  
                            "last updated": "04/29/2021"},

                      1002: {"name": "16 Awg Wire", 
                            "price": 10,
                            "category": "Wires",
                            "quantity": 2,
                            "length (ft)" : 20,
                            "Description" : "N/A",
                            "last updated": "04/29/2021"},

                      1003: {"name": "1/2 inch Pipe", 
                            "price": 15,
                            "category": "Pipes",
                            "quantity": 4,
                            "length (ft)": 3, 
                            "Description" : "A metal pipe",
                            "last updated": "04/29/2021"},

                      1004: {"name": "L16",  
                            "price": 0.60,
                            "category": "Small Items",
                            "quantity": 50,
                            "Description" : "N/A" ,
                            "last updated": "04/29/2021"},

                      1005: {"name": "100ohm Resistor", 
                            "price": 0.14,
                            "category": "Electronics",
                            "quantity": 100,
                            "Description" : "Brown Black Brown Gold",
                           "last updated": "04/29/2021"},
                      }
My Inventory menu/main script:
#---------------------------------------------------------------
                            #Overview
#---------------------------------------------------------------
#User is prompted to a login screen 
#User enters thier name
#If their name is in the database they may access the program

#User is then taken to the main menu
#From there they can checkout a part,
#Return a part,
#Search for a part,
#And view all the parts in stock  

#Admins can:
#Add new items,
#Delete items,
#Update items,
#And view everything in stock
#---------------------------------------------------------------

#---------------------------------------------------------------
#                        Login Menu
#---------------------------------------------------------------
def login():
    while True:
        print('=============================\n''\tLogin\n''=============================')
        user = input("Enter your name (Case Sensitive): ")
        password = int(input("Enter your password: "))
        #If the credentials are validated -> Run validate function -> Go to according menu
        #Else -> Print error message and show login screen again
        if validate(user, password):
            break
        else:
            print('You are not registered to use this program')
#---------------------------------------------------------------

#---------------------------------------------------------------
#                Validate Function -> Validates user 
#---------------------------------------------------------------
#Pass in user and password variables from login function
#Check if the user is valid (registered in database) -> Give access to inventory program 
#If user is not valid -> return to login
#-->TODO: Write the user and current date to a file to keep track of logins
def validate(user, password):
    if StandardUsers().get(user) == password:
        print('Success! You are now logged in as: ' + user)
        mainMenu()
        return True

    if SuperUsers().get(user) == password:
        print('Success! You are now logged in as: ' + user)
        adminMenu()
        
    else:
        print('You are not registered to use this program')
        login()
#---------------------------------------------------------------

#---------------------------------------------------------------
#                          Admin Menu 
#---------------------------------------------------------------
def adminMenu():
    print('=============================')
    print('= Inventory Admin Menu =')
    print('=============================')
    print('(1) Add New Item to Inventory')
    print('(2) Remove Item from Inventory')
    print('(3) Update Inventory')
    print('(4) Search Item in Inventory')
    print('(5) Print Inventory Report')
    print('(6) Log Out')
    print('(Q) Quit')
    CHOICE = input("Enter choice: ")
    adminMenuSelection(CHOICE)

def adminMenuSelection(CHOICE):
    if CHOICE == '1':
        #Call add item function
    elif CHOICE == '2':
        #Call remove item function
    elif CHOICE == '3':
        #Call update item function
    elif CHOICE == '4':
        #Call search item function
    elif CHOICE == '5':
        #Call print item function
    elif CHOICE == '6':
        login()
    elif CHOICE == 'q' or CHOICE == 'Q':
        exit()
#---------------------------------------------------------------
It won't work because you don't have a way to modify the inventory. Remove items and they will reappear when the program is run again. Add items and they will be missing when you restart the program. You should use a real database and something like simplesql perform database queries.
visit some of these packages: https://pypi.org/search/?q=inventory
Find one you like, and click on homepage to take a look at the code used to create the package.
(Apr-29-2022, 10:45 PM)deanhystad Wrote: [ -> ]It won't work because you don't have a way to modify the inventory. Remove items and they will reappear when the program is run again. Add items and they will be missing when you restart the program. You should use a real database and something like simplesql perform database queries.

Can't you overwrite Dictionaries or update them? Will those changes not stick?

But I'll give SQLite a try.



Just a quick question:
So I'm making a quick table and I'm wondering if I make a Column called "Retail Price" will I be able to use python to put a double value in that column?
[For example If I make a servo item with a price of $10 then python should add $15 to the retail price column]

#Create a database (inventory.db)
connection = sqlite3.connect("inventory.db")
cursor = connection.cursor()

cursor.execute('''CREATE TABLE Items
         (ID INT PRIMARY KEY    NOT NULL,
         NAME           TEXT    NOT NULL,
         Quantity       TEXT    NOT NULL,
         Price          DOUBLE  NOT NULL,
         Category       TEXT,
         Description    TEXT,
         Sell Price     DOUBLE);''')