[Tutor] if/then statement and sql count rows

Alan Gauld alan.gauld at btinternet.com
Thu Apr 30 10:46:49 CEST 2015


On 30/04/15 03:59, Spencer For Friends wrote:

>      class PriceCheck(object):
>          def __init__(self, db):
>              self.conn = sqlite3.connect(db)
>              self.c = self.conn.cursor()
>
>
>          def query(self, arg, cardname):
>              self.c.execute(arg, cardname)
>              return self.c

You are returning the cursor object here.
That's probably not what the user of your code wants,
it would be better to return the actual result.
You can get that using fetchone() or fetchall()
Since count() can only return one value fetchone()
would be best here.

return self.c.fetchone()

>          def __del__(self):
>              self.conn.close()
>
>      def display():
>      #Connect To DB and Get Price of Matched book.
>          getbookprice = PriceCheck("arbbase.sqlite")
>          for row in getbookprice.query(sql, ([book_name])):

Insert a debug statement here:

print type(row), ':', row

And see if what you get is what you expect.

>              if row == 0:
>                  print 'no row was found'
>              else:
>                 print 'Yep its there!'
>                  print row

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list