How to do select count (*) with Python and mysql.connector

3

The library I'm using is this .

This is the code:

cur.execute("""SELECT COUNT(*) as total FROM tabela as t WHERE ... """, (v1, v2))

for (total) in cur:
    if total > 0:
        print('Existe')

The problem is that there is never any value.

PS: There are data in the table to bring a count > 0

What's wrong with this?

    
asked by anonymous 07.01.2016 / 01:25

1 answer

2

Since it's a count, you do not have to use for to get the data.

Another thing is that the total is not an integer, it is a tuple

Either way do this:

cur.execute("""SELECT COUNT(*) as total FROM tabela as t WHERE ... """, (v1, v2))
(total,) = cursor.fetchone()
if total:
    ...
    
07.01.2016 / 01:29