mysqlDB, TypeError: unsupported operand type(s) for /: 'str' and 'Decimal'

552 views Asked by At

Here is my SQL:

SELECT * FROM (SELECT sk, SUM(qty) AS cnt FROM order WHERE sk LIKE '%s%s' AND days <= '%s' GROUP BY sk ORDER by cnt DESC) AS tmp WHERE tmp.cnt >3;

and here is my code:

result = cur.fetchall()
for sk, cnt in result:
    rat = cnt/total

then I get a error:

TypeError: unsupported operand type(s) for /: 'str' and 'Decimal'

I have no idea why cnt is type str instead of int.

1

There are 1 answers

0
Jack.W On

Here is the problem: each row of the fetchall() is a dict which looks like {'sk': u'S0013', 'cnt': Decimal('1971')} so cnt in result is the key of the dict instead of value..