views:

41

answers:

1

I ran into an interesting problem. I have a MySQL database that contains some doubles with very precise decimal values (for example, 0.00895406607247756, 17 decimal places). This is scientific data so this high level of precision is very important.

I'm using MySQLdb in Python to select data from the database:

cursor.execute("SELECT * FROM " ...etc...)

for n in range(cursor.rowcount):
    row = cursor.fetchone()
    print row

For some reason, when it gets to my very precise decimals, they've been truncated to a maximum of 14 decimal places. (i.e. the previous decimal becomes 0.00895406607248)

Is there any way to get the data from MySQLdb in its original form without truncating?

+1  A: 

What MySQL datatype are you using to store the data? Is it DECIMAL(18,17)? DECIMALs have up to 65 digits of precision.

If you set the MySQL data type to use DECIMAL(...), then MySQLdb will convert the data to a Python decimal.Decimal object, which should preserve the precision.

unutbu
I'm using DOUBLE. I'll give DECIMAL a shot.
Ben Morris
Thanks! That seemed to do the trick.
Ben Morris