0

I used the followng code to trigger IF statement based on the value of row[0] my problem is always IF statement give me wrong output even if the value is greater than 0 its keep printing NO FAILURE .

conn = psycopg2.connect(conn_string)
        cursor = conn.cursor()
        cursor.execute("select count(*) as result from events_log where match_event_timestamp  > (NOW() - INTERVAL '147 hours');")
        row = cursor.fetchone()
        cursor.execute("SELECT COUNT(*) AS result FROM events_log  WHERE status = 'FAILURE' AND match_event_timestamp  > (NOW() - INTERVAL '24 hours');")
        row += cursor.fetchone()
        logger.info("Number of Events in last 24hr : %s ; Number of Event FAILURE  in last 24h: %s" % (row[0],row[1]))
        output = { 'api_key':'jhfsdf', 'data': { 'item' : [
                        {'text': 'Today ', 'value': row[0]},
                        {'text': 'Current Month ', 'value': row[1]}
                    ] } }

        if row[0] > '0':
                print('FAILURE')
        else:
                print("NO FAILURE")

what did i missed in IF statement here ?

3
  • 1
    You mean greater than 0 not '0' right? Commented May 29, 2016 at 14:00
  • 1
    i dont know what row[0] contains... but what happens if you convert them to int before evaluating the condition of the if/else ? like if int(row[0]) > 0: Commented May 29, 2016 at 14:00
  • fixed the problem :) Commented May 29, 2016 at 14:05

1 Answer 1

1

'0' is a string use 0. Like this:

if( row[0] > 0 ):
   ...

Otherwise you get this:

TypeError: unorderable types: int() > str()

which means you may not compare an int to a string.

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

Comments

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.