Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Problem with sqlite3 and Decimal Date: Sat, 12 Dec 2015 18:45:53 +1100 Lines: 24 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 1tQvOs2A6KG481CDFwc32g0udzsdCjCUZlbVL5vMKRKw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'subject:sqlite3': 0.07; 'integers': 0.09; 'internally': 0.09; 'python': 0.10; 'arithmetic.': 0.16; 'fixed-point': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scaled': 0.16; 'sqlite3': 0.16; 'subject:Problem': 0.16; 'wrote:': 0.16; 'integer': 0.18; '2015': 0.20; 'to:name:python-list@python.org': 0.20; 'amounts': 0.22; 'precise': 0.22; 'pass': 0.22; 'trying': 0.22; 'fit': 0.23; 'bit': 0.23; 'dec': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'required.': 0.26; 'switch': 0.27; 'message-id:@mail.gmail.com': 0.27; 'back.': 0.27; 'converting': 0.27; 'values': 0.28; 'arithmetic': 0.29; 'decimal': 0.29; 'figured': 0.29; 'workaround': 0.29; 'objects': 0.29; 'probably': 0.31; 'another': 0.32; 'point': 0.33; 'problem': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'mix': 0.35; 'supports': 0.35; 'but': 0.36; 'needed': 0.36; 'received:209.85': 0.36; 'data.': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'or,': 0.38; 'end': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; "you'll": 0.61; 'back': 0.62; 'more': 0.63; 'effective.': 0.66; 'talking': 0.67; 'frank': 0.72; 'dollar': 0.76; 'article': 0.77; 'chrisa': 0.84; 'clearly.': 0.84; 'ion': 0.84; 'hand,': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=dROArrcMgW+JDZ61lx53k/X0UxIWEcceymW9+gaCpr0=; b=G9PzHeAmzmsqfGxdVLu4yixtsMkelsqyLN+uBtVV/5IZcUP2wV8X6uqPXC0nsCHCSL omlNQqEICTpMZ3Myca/OQjL0mSyF7dxp1+IbGZ3/D19xBzf6+9sM1Kn4EAgC/HjywF2J bKSkQgpiU3MFDyVMcr0+sQQ7tf/pNF9031oaHx7xB5ENRiyonpUiBdp63GmztBuJpazy z4BgJVspbSBLvfsFNHoQJG7sxb1K9TYqnEaSO+a3kccfLeQKamSGHMwTdDbOJj9HF4gK 7FMgY75RE1zcGOGqrVGtz4EKC2zV6JjVnFbYpTTMUnWxNl71ENzhGAiK1ZmSOU/m2JOj 0cKw== X-Received: by 10.50.61.164 with SMTP id q4mr8629111igr.13.1449906353699; Fri, 11 Dec 2015 23:45:53 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100323 IOn Sat, Dec 12, 2015 at 6:31 PM, Frank Millman 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