Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Laura Creighton Newsgroups: comp.lang.python Subject: Problem with sqlite3 and Decimal (fwd) Date: Fri, 11 Dec 2015 14:45:42 +0100 Lines: 94 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de 1BuF5yl/VyPZ1/CprOnwEQdzZ4eNnMPBbdjwoyyaFvxg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.05; 'seemed': 0.07; 'subject:sqlite3': 0.07; 'cc:addr:python-list': 0.09; '-------': 0.09; 'adapter': 0.09; 'lines:': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'str)': 0.09; 'switches': 0.09; 'python': 0.10; 'result.': 0.15; 'conn': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'lambda': 0.16; 'loops': 0.16; 'message-id:@fido.openend.se': 0.16; 'oddity': 0.16; 'places.': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sqlite3': 0.16; 'subject:Problem': 0.16; 'true:': 0.16; 'weird.': 0.16; 'string,': 0.18; 'runs': 0.18; 'windows': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '+0200': 0.20; 'int,': 0.22; 'cc:no real name:2**0': 0.22; 'dec': 0.23; 'tried': 0.24; 'import': 0.24; 'example': 0.26; 'appreciated.': 0.27; 'followed': 0.27; 'fri,': 0.27; 'values': 0.28; 'fine': 0.28; 'arithmetic': 0.29; 'decimal': 0.29; 'received:se': 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; 'next': 0.35; 'subject:': 0.35; 'asking': 0.35; 'problem.': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.36; 'forwarded': 0.36; 'two': 0.37; 'charset:us-ascii': 0.37; 'itself': 0.38; 'end': 0.39; 'from:': 0.39; 'url:mail': 0.40; 'subject:with': 0.40; 'still': 0.40; 'header:Message-Id:1': 0.61; 'back': 0.62; 'converter': 0.66; 'here': 0.66; 'email name:python-list': 0.67; 'skip:6 10': 0.67; 'frank': 0.72; '3.4': 0.84; 'bal': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openend.se; s=default; t=1449841544; bh=dGE5/6sO2TVKD8h4DX4WqSmPdoDGSjjwNqaiOIkhuLU=; h=To:cc:From:Subject:Date:From; b=ih76OxhBcfMXF4nFCQ5Npa74eMVVPG6GpiOOOo/EMjHNt1PSCcyfr7QdnDp0PS89n rKAXjWvHBOS/OufXNuv0PxZ+W8t6ss+Xi2SzNYSLYt3UWVB1UrmAT1o/8pTGNrBR+u W3XcS8y9WgdAViP7GQ5k+EXKVKdyoXQngo67Z38w= Content-ID: <24999.1449841542.1@fido> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [82.96.5.2]); Fri, 11 Dec 2015 14:45:44 +0100 (CET) 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:100260 =46rom 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-decim= al-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 =3D sqlite3.connect(':memory:', detect_types=3Dsqlite3.PARSE_DECLTYPE= S) cur =3D conn.cursor() cur.execute("CREATE TABLE fmtemp (acno INT, bal DEC)") cur.execute("INSERT INTO fmtemp (acno, bal) VALUES (?, ?)", ('A001', D('0'))) sql1 =3D "SELECT bal FROM fmtemp" sql2 =3D "UPDATE fmtemp SET bal =3D bal + ?" while True: print(cur.execute(sql1).fetchone()[0]) cur.execute(sql2, (D('123.45'),)) q =3D input() if q =3D=3D 'q': break """ It initialises a decimal value in the database, then loops adding a decima= l 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 i= s 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. Any suggestions will be much appreciated. Frank Millman = - -- = https://mail.python.org/mailman/listinfo/python-list ------- End of Forwarded Message