0

I am, having problems trying to sore the output of an sqlite query in a python variable to keep track of a total. I have looked around and seen examples using the c.fetchone() method but I cannot seem to get it to work , I keep getting the error TypeError: 'NoneType' object has no attribute '___getitem____' . I don't understand what this means . can anyone help?

    i = -1
    i = i + 1

    self.t = pytesseract.image_to_string(PIL.Image.open('test2.jpg'))
    self.c.execute("SELECT name FROM item WHERE code = '%s'" % self.t)
    self.listbox.insert(i,self.c.fetchone())
    self.listbox1.insert(i,"£")
    self.c.execute("SELECT price FROM item WHERE code = '%s'" % self.t)
    self.listbox2.insert(i,self.c.fetchone())

    self.y=self.c.fetchone()[i]
    print self.y

1 Answers1

1

You need to fetch the value once and reuse:

self.c.execute("SELECT price FROM item WHERE code = %s", (self.t, ))

value = self.c.fetchone()[0]
self.listbox2.insert(i, value)
self.y = value

Note how I'm passing the self.t value into the query - this is called parameterization and should be preferred comparing to using string formatting to make SQL queries.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • After changing the code to this i get an error message on the line self.c.execute("SELECT price FROM item WHERE code = %s", (self.t, )) Operational error near % – user3676186 Mar 20 '16 at 18:40
  • http://stackoverflow.com/questions/775296/python-mysql-with-variables i have been reading here and it seems to be the same syntax but im still getting the Operational error near "%" : Syntax error – user3676186 Mar 20 '16 at 19:10