[Tutor] if/then statement and sql count rows

Spencer For Friends smarugg at gmail.com
Thu Apr 30 04:59:48 CEST 2015


Hi All,

I'm attempting to perform an if then statement based on the results of a
sql count rows query. The query is returning the proper values however the
if/then statement seems to be ignoring the returned value of the sql
statement.

Here is my code.

    import sqlite3

    # Define SQL statement to select all Data from Column Name
    sql = "SELECT Count(*) FROM arbtable WHERE Name = ?"

    book_name = 'Winged Coat'

    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

        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])):
            if row == 0:
                print 'no row was found'
            else:
               print 'Yep its there!'
                print row



    display()

The program here is that my result, as evidenced by "print row" at the
bottom of the code is 0. Since it is zero according to my if statement it
should print 'no row was found', but instead it prints the else statement
value of 'yes its there!'

I've researched extensively and think that possibly the fetchone statement
is what I need, but I can't seem to figure out how to properly apply this
to my code to test it.

Any help would be highly appreciated.

Thank you kindly

Sour Jack


More information about the Tutor mailing list