1

I have this Python script:

s = stdscr.getstr(0,0, 20) #input length last number
            c = db.execute("""SELECT "debit" FROM "members" WHERE "barcode" = '%s' LIMIT 1""" % (s,))
            for row in c:
                    print row

                    if row == '(0,)':
                            #display cross
                            print 'Tick'
                    else:
                            #display tick
                            print 'Cross'

Where it is asking for a barcode input, and matching the debit field in the database.

The "print row" command returns "(0,)" but when I try to match it, I always get "Cross" as the output, which is not the intended result. Is there a semantic I'm obviously not observing?

Many thanks!

1 Answer 1

1

The variable row is a tuple, and '(0,)' is its string representation. Your are comparing a variable with its string representation, which cannot work.

You need to compare it to the tuple value

if row == (0,):

Simply remove the quote marks.

Alternatively, you can write

if row[0] == 0:

which will avoid the creation of a tuple just for the comparison. As noted by @CL., row will never be an empty tuple so extracting row[0] is safe.

Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't row[0] == 0 be better?
@CL. The disadvantage I see is that you need then to catch IndexError: tuple index out of range then. But maybe execute will never return an empty tuple in this case. Am I missing something? Do you see a clear advantage to your approach?
The value might be NULL, but it will always be there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.