Problem with sqlite3 and Decimal

Frank Millman frank at chagford.com
Sun Dec 13 00:00:26 EST 2015


"Frank Millman"  wrote in message news:n4gigr$f51$1 at ger.gmane.org...

> I have found a workaround for my problem, but first I needed to understand
> what was going on more clearly. This is what I have figured out.
>
[...]
>
> The reason for the '#' in the above function is that sqlite3 passes the
> current value of 'balance' into my function, and it has a bad habit of
> trying to second-guess the data-type to use. Even though I store it as a
> string, it passes in an integer or float. Prefixing it with a '#' forces
> it to remain as a string.

Well that theory did not last very long!

As soon as I applied this to my live app, I found that I am using the 
database to perform arithmetic all over the place - calculating tax, 
exchange rates, etc. I always round the result once it arrives from the 
database, so there was no rounding problem.

With the prefix of '#' the calculations all just crash, and return null 
values.

My new solution is to pass a 'scale' factor into my aggregate function. The 
function uses the Decimal quantize method to round the result before 
returning. So far it seems to be working.

Frank





More information about the Python-list mailing list