Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100323

Re: Problem with sqlite3 and Decimal

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Problem with sqlite3 and Decimal
Date 2015-12-12 18:45 +1100
Message-ID <mailman.174.1449906356.12405.python-list@python.org> (permalink)
References <n4ei3l$b98$1@ger.gmane.org> <n4gigr$f51$1@ger.gmane.org>

Show all headers | View raw


IOn Sat, Dec 12, 2015 at 6:31 PM, Frank Millman <frank@chagford.com> wrote:
> 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.
>
> 1. The solution in the SO article is a bit of sleight of hand, though very
> effective. It does not create a Decimal type in sqlite3. It simply provides
> a way of converting Decimal objects to strings when you pass them into the
> database, and converting them back to Decimal types when you read them back.
>
> 2. This works if you only use sqlite3 as a storage mechanism, and use Python
> to perform any arithmetic required. It fails when you try to use sqlite3 to
> perform arithmetic, as it uses floating point internally and suffers from
> the same problem that Python does when trying to mix floating point and
> precise decimal representation.

There's another possibility, and that's fixed-point arithmetic. You
store the numbers as integers - probably cents, if you're talking
about dollar amounts - and as long as the scaled values fit inside the
available integer type (probably 64-bit), you'll be fine.

Or, of course, you could switch to a database back end that actually
supports NUMERIC data.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Problem with sqlite3 and Decimal Chris Angelico <rosuav@gmail.com> - 2015-12-12 18:45 +1100

csiph-web