Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Igor Korot Newsgroups: comp.lang.python Subject: Re: Problem with sqlite3 and Decimal (fwd) Date: Fri, 11 Dec 2015 09:04:59 -0500 Lines: 99 Message-ID: References: <201512111345.tBBDjgae025001@fido.openend.se> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 1tjUt4ZPzD62XBp0da8pawL+BJhVfQr4uVwVh14LZu9A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.05; 'seemed': 0.07; 'subject:sqlite3': 0.07; '-------': 0.09; 'adapter': 0.09; 'creighton': 0.09; 'lines:': 0.09; 'str)': 0.09; 'switches': 0.09; 'python': 0.10; 'result.': 0.15; 'conn': 0.16; 'lambda': 0.16; 'loops': 0.16; 'oddity': 0.16; 'places.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sqlite3': 0.16; 'subject:Problem': 0.16; 'true:': 0.16; 'weird.': 0.16; 'wrote:': 0.16; 'laura': 0.18; 'string,': 0.18; 'runs': 0.18; 'windows': 0.20; '2015': 0.20; '+0200': 0.20; 'to:name:python-list@python.org': 0.20; 'int,': 0.22; 'am,': 0.23; 'dec': 0.23; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'appreciated.': 0.27; 'followed': 0.27; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'values': 0.28; 'fine': 0.28; 'arithmetic': 0.29; 'decimal': 0.29; 'objects': 0.29; 'url:mailman': 0.30; 'position.': 0.30; 'date:': 0.31; 'skip:s 30': 0.31; 'another': 0.32; 'noticed': 0.32; 'table': 0.32; 'point': 0.33; 'problem': 0.33; 'url:python': 0.33; 'url:listinfo': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'subject:': 0.35; 'asking': 0.35; 'problem.': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'email addr:python.org': 0.36; 'to:addr:python-list': 0.36; 'forwarded': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209.85.213': 0.37; 'itself': 0.38; 'received:209': 0.38; 'thank': 0.38; 'hi,': 0.38; 'end': 0.39; 'from:': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'still': 0.40; 'save': 0.60; 'back': 0.62; 'you.': 0.64; 'converter': 0.66; 'here': 0.66; 'email name:python- list': 0.67; 'skip:6 10': 0.67; 'saving': 0.70; 'frank': 0.72; '3.4': 0.84; 'bal': 0.84 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=uZ5t/BGVTcGhgik7XbJdsGKJx8FlBXJ60oTgw7DbHCg=; b=MZr7jpu6QshuWgzamxMYclh9A24S2lQXPESHrAxD25TkJrka66mv5CmX8yk73Z+bdr RCvF9vfh5OdT2narTBIrKeTVRKYwYpuE2EqkY0viGUJOZs6b8k1XQY6gB2edz0J+wm4P S52OAmAo9EhphgV2hCZvh15C0aXuQILWvr7X6Ed59CTfEGtgNFq7PI65MmZPOFRG9aYb YDbjYMBiu5+ycrHflJLR1jiNNV6W0+tFzMIxFaA6S6qW7UIAH+faWL0MtwPusLnzw/Rw VagY1qTCRXIc2hkxBMLx16AE+E+mJs6nIN1q0C7jS+h/dQwspysY980Dxilx8z+wp/7i JikA== X-Received: by 10.31.42.198 with SMTP id q189mr2062017vkq.126.1449842699767; Fri, 11 Dec 2015 06:04:59 -0800 (PST) In-Reply-To: <201512111345.tBBDjgae025001@fido.openend.se> 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:100261 Hi, On Fri, Dec 11, 2015 at 8:45 AM, Laura Creighton wrote: > From python-list. > Very weird. > Another reason not to use sqlite3 > > ------- Forwarded Message > > To: python-list@python.org > From: "Frank Millman" > Subject: Problem with sqlite3 and Decimal > Date: Fri, 11 Dec 2015 11:21:53 +0200 > Lines: 71 > > Hi all > > I need to store Decimal objects in a sqlite3 database, using Python 3.4 on > Windows 7. > > I followed the instructions here - > > http://stackoverflow.com/questions/6319409/how-to-convert-python-decimal-to-sqlite-numeric > > It seemed to work well, but then I hit a problem. Here is a stripped-down > example - > > """ > from decimal import Decimal as D > import sqlite3 > > # Decimal adapter (store Decimal in database as str) > sqlite3.register_adapter(D, lambda d:str(d)) > > # Decimal converter (convert back to Decimal on return) > sqlite3.register_converter('DEC', lambda s: D(s.decode('utf-8'))) > > conn = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES) > cur = conn.cursor() > > cur.execute("CREATE TABLE fmtemp (acno INT, bal DEC)") > cur.execute("INSERT INTO fmtemp (acno, bal) VALUES (?, ?)", ('A001', > D('0'))) > > sql1 = "SELECT bal FROM fmtemp" > sql2 = "UPDATE fmtemp SET bal = bal + ?" > > while True: > print(cur.execute(sql1).fetchone()[0]) > cur.execute(sql2, (D('123.45'),)) > q = input() > if q == 'q': > break > """ > > It initialises a decimal value in the database, then loops adding a decimal > value and displaying the result. > > It runs fine for a while, and then the following happens - > > 5802.15 > > 5925.6 > > 6049.05 > > 6172.4999999999 > > 6295.9499999999 > > It consistently switches to floating point at the same position. If you > carry on for a while, it reverts back to two decimal places. > > If I initialise the value as D('6049.05'), the next value is 6172.5, so it > is not the number itself that causes the problem. > > I tried displaying the type - even when it switches to 6172.49999999, it is > still a Decimal type. > > I noticed one oddity - I am asking sqlite3 to store the value as a string, > but then I am asking it to perform arithmetic on it. Is there a reason you are saving it as the string? What happens when you save it as decimal? Thank you. > > Any suggestions will be much appreciated. > > Frank Millman > > > - -- > https://mail.python.org/mailman/listinfo/python-list > > ------- End of Forwarded Message > -- > https://mail.python.org/mailman/listinfo/python-list