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


Groups > comp.lang.python > #100259

Problem with sqlite3 and Decimal

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Problem with sqlite3 and Decimal
Date Fri, 11 Dec 2015 11:21:53 +0200
Lines 72
Message-ID <mailman.136.1449839552.12405.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; format=flowed; charset="iso-8859-1"; reply-type=original
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de YApOkFAecVNbvr2VLim27APfeLDHyxAGcBMqSXbslqnA==
Return-Path <python-python-list@m.gmane.org>
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; 'adapter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 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:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'sqlite3': 0.16; 'subject:Problem': 0.16; 'true:': 0.16; 'string,': 0.18; 'runs': 0.18; 'windows': 0.20; 'int,': 0.22; 'tried': 0.24; 'import': 0.24; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'appreciated.': 0.27; 'followed': 0.27; 'values': 0.28; 'fine': 0.28; 'arithmetic': 0.29; 'decimal': 0.29; 'objects': 0.29; 'position.': 0.30; 'skip:s 30': 0.31; 'noticed': 0.32; 'table': 0.32; 'point': 0.33; 'skip:d 20': 0.34; 'next': 0.35; 'asking': 0.35; 'problem.': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'two': 0.37; 'received:org': 0.37; 'itself': 0.38; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'still': 0.40; 'back': 0.62; 'converter': 0.66; 'here': 0.66; 'skip:6 10': 0.67; 'frank': 0.72; '3.4': 0.84; 'bal': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 197.89.230.120
X-MSMail-Priority Normal
Importance Normal
X-Newsreader Microsoft Windows Live Mail 15.4.3502.922
X-MimeOLE Produced By Microsoft MimeOLE V15.4.3502.922
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100259

Show key headers only | View raw


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.

Any suggestions will be much appreciated.

Frank Millman 

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


Thread

Problem with sqlite3 and Decimal "Frank Millman" <frank@chagford.com> - 2015-12-11 11:21 +0200

csiph-web