Oluderi Oluderi - 1 year ago 174
MySQL Question

TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

I am using Python 2.7 and MySQLdb. I get this error on this code:

Value = 5
x.execute("SELECT number from Testing where id ='%s';" % Value)
data = x.fetchall()
print (data)
data = data[0][0]
data = data + 0.5
x.execute(""" UPDATE Testing SET number = %s WHERE id = %s """, (data, Value))

The error occurs on the line:
data = data + 0.5

TypeError: unsupported operand type(s) for +: 'decimal' and 'float'.

The number is a
. I have seen other questions with this error but not for adding. Plus, I think some people will have the same problem if they are new to Python and can't understand more advanced Python coding for similar problem questions. Could you please help me? Thanks in advance.

Answer Source

There doesn't appear to be support for that operation, you will have to construct a Decimal object first and add this:

In [132]:

import decimal
d = decimal.Decimal(23.0)
d = d + decimal.Decimal(0.5)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download