Whats the correct way to get the number or rows returned by this query? I’m specifically looking to see if no results are returned.
JavaScript
x
7
1
sql = 'SELECT count(*) from table WHERE guid = %s;'
2
data=[guid]
3
cur.execute(sql,data)
4
results = cur.fetchone()
5
for r in results:
6
print type(r) # Returns as string {'count': 0L} Or {'count': 1L}
7
Thanks.
Advertisement
Answer
results
is itself a row object, in your case (judging by the claimed print
output), a dictionary (you probably configured a dict-like cursor subclass); simply access the count
key:
JavaScript
1
3
1
result = cur.fetchone()
2
print result['count']
3
Because you used .fetchone()
only one row is returned, not a list of rows.
If you are not using a dict(-like) row cursor, rows are tuples and the count value is the first value:
JavaScript
1
3
1
result = cur.fetchone()
2
print result[0]
3