Python Forum
Help with python 'not subscriptable' error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with python 'not subscriptable' error
#1
Hello,

I have this chunk of code from my PyQt GUI that gets a result from a SQLite table and uses that result to do a math calculation.

Snippet:
else: 
                        #Connect to the inventory database (inventory.db)
                        connection = sqlite3.connect(InventoryDatabase)
                        cursor = connection.cursor()
                        cursor.execute("SELECT Sell_Price_$ FROM items WHERE Name = ?",(SelectedItemName,))
                        connection.commit()
                        SellPrice = cursor.fetchone()
                        
                        # #Update the quantity
                        # cursor.execute("UPDATE items SET Quantity = Quantity - ? WHERE Name = ?",(SelectedItemQuantity, SelectedItemName,))
                        # connection.commit()
                        
                        #Close the connection
                        connection.close()

                        print('Selcted Item Quantity: ', SelectedItemQuantity)

                        x = float(SelectedItemQuantity)
                        print('Float Value = ', x)
                        y = SellPrice[0]
                        print("y = ",y)
                        z = float(y)
                        print('x*z= ', x*z)
                        MaterialUsedPrice2 = x*z

                        #Calculate the Price of Material Used
                        #MaterialUsedPrice = SelectedItemQuantity * SellPrice
                        print('The Total Price for the', '(',x,')', SelectedItemName, ' Used is: $', MaterialUsedPrice2)
Output:
Output:
2 , 1/2" PVC Coupling ('PVC',) Selcted Item Quantity: 2 Float Value = 2.0 y = 1.5 x*z= 3.0 The Total Price for the ( 2.0 ) 1/2" PVC Coupling Used is: $ 3.0 #I don't know why this gets printed (again...Since it's already been printed above (Selcted Item Quantity: 2 Float Value = 2.0) and I don't know why the values are 0) 0 , - None Selcted Item Quantity: 0 Float Value = 0.0 y = SellPrice[0] TypeError: 'NoneType' object is not subscriptable
I get this error and I don't know why (When I do print("y = ",y) I get 1.5, which is the correct value, so why am I getting this error?)
Error:
y = SellPrice[0] TypeError: 'NoneType' object is not subscriptable
Why do I get this error and how do I fix it?

Thanks in advance.

Full function (If it helps):
    def SubmitClicked(self):
        #Get User Inputted Quantity & Material
        #Probably going to have to do it the long way: Do all 22 comboBoxes individually. 
        for widget in self.MaterialUsedFrame.children():        
            if isinstance(widget, QSpinBox):
                    SelectedItemQuantity = widget.text()
            if isinstance(widget,QComboBox):
                    SelectedItemName = widget.currentText()
                    print(SelectedItemQuantity, ',' ,SelectedItemName)
        
                    #Connect to the inventory database (inventory.db)
                    connection = sqlite3.connect(InventoryDatabase)
                    cursor = connection.cursor()
                    cursor.execute("SELECT Main_Category FROM items WHERE Name = ?",(SelectedItemName,))
                    connection.commit()
                    Result = cursor.fetchone()
                    #Close the connection
                    connection.close()
                    print(Result)

                    if Result == ('Wire',):
                        #Connect to the inventory database (inventory.db)
                        connection = sqlite3.connect(InventoryDatabase)
                        cursor = connection.cursor()
                        cursor.execute("SELECT Price_Per_Ft FROM items WHERE Name = ?",(SelectedItemName,))
                        connection.commit()
                        PricePerFt = cursor.fetchone()
                        #Close the connection
                        connection.close()
                        print(PricePerFt)
                        
                        #Might be better off making all those SpinBoxes Labels
                        #& Just setting the text to the price
                        for widget in self.MaterialPriceFrame.children():        
                            if isinstance(widget, QDoubleSpinBox):
                                widget.setValue(80.80)

                    else: 
                        #Connect to the inventory database (inventory.db)
                        connection = sqlite3.connect(InventoryDatabase)
                        cursor = connection.cursor()
                        cursor.execute("SELECT Sell_Price_$ FROM items WHERE Name = ?",(SelectedItemName,))
                        connection.commit()
                        SellPrice = cursor.fetchone()
                        
                        # #Update the quantity
                        # cursor.execute("UPDATE items SET Quantity = Quantity - ? WHERE Name = ?",(SelectedItemQuantity, SelectedItemName,))
                        # connection.commit()
                        
                        #Close the connection
                        connection.close()

                        print('Selcted Item Quantity: ', SelectedItemQuantity)

                        x = float(SelectedItemQuantity)
                        print('Float Value = ', x)
                        y = SellPrice[0]
                        print("y = ",y)
                        z = float(y)
                        print('x*z= ', x*z)
                        MaterialUsedPrice2 = x*z

                        #Calculate the Price of Material Used
                        #MaterialUsedPrice = SelectedItemQuantity * SellPrice
                        print('The Total Price for the', '(',x,')', SelectedItemName, ' Used is: $', MaterialUsedPrice2)
#----------------------------------------------------------------------------------------------------
Reply
#2
The error is because SellPrice has the value None. You need to work out why that is - looks like no value was returned from your database query.
Reply
#3
Quote:fetchone()
If row_factory is None, return the next row query result set as a tuple. Else, pass it to the row factory and return its result. Return None if no more data is available.
row_factor is None because NO MORE DATA IS AVAILABLE. Maybe there never was any data to start with, or maybe you already retrieved all the data. I'm guessing the former looking at your code. It doesn't matter the reason why, what matters is that the documentation for fetch_one() says that it might return None, and your program does nothing to prevent that highly likely possibility from crashing the program.
ndc85430 likes this post
Reply
#4
I have always used double equal signs
WHERE Name == ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 762 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,274 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  'int' object is not subscriptable after API call ed8484 1 1,824 Sep-18-2021, 02:06 PM
Last Post: ed8484
  Message='int' object is not subscriptable DanielCook 4 2,333 Aug-10-2021, 05:21 PM
Last Post: deanhystad
  Bool Object is not Subscriptable quest 1 4,176 May-02-2021, 11:12 AM
Last Post: Yoriz
  Float Object is not Subscriptable quest 2 2,907 Apr-20-2021, 09:28 AM
Last Post: quest
  TypeError: 'NoneType' object is not subscriptable Jmekubo 6 27,354 Sep-08-2020, 10:03 AM
Last Post: DigiTMG
  TypeError: 'type' object is not subscriptable Stef 1 4,543 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  'NoneType' object is not subscriptable Justchse 4 3,715 Aug-01-2020, 06:18 PM
Last Post: Justchse
  'AxesSubplot' object is not subscriptable amitsinbox 0 8,033 Apr-30-2020, 07:57 AM
Last Post: amitsinbox

Forum Jump:

User Panel Messages

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