Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:23:59+00:00 2026-05-22T14:23:59+00:00

Using sqlite3 as the database, I want to keep a couple of decimal.Decimal values,

  • 0

Using sqlite3 as the database, I want to keep a couple of decimal.Decimal values, one a percentage the other a dollar amount. However I saw problems when using sum(amount) on over 20,000 entries. It was several dollars off.

I thought of using an adapter to save the number of cents and then aggregates should work.

sqlite3.register_adapter(decimal.Decimal, lambda x:str(x))
sqlite3.register_adapter(decimal.Decimal, lambda x:int(x*100))

However, I would now need two classes as I can not use the same class. Trying to subclass Decimal became an issue as it uses Decimal within itself. Fine. I’ll just copy decimal.py and replace every occurance of Decimal|decimal with Money|money.

$ copy decimal.py money.py
$ sed -e "s/Decimal/Money/g" -e "s/decimal/money/g" -i money.py
$ money.py

All the unit tests work. I try it now and I am getting a “probably unsupported type” error. I change around the converters, use basic types. I just can not get it working right and I don’t have a better idea for the problem.

I need some help and have a sample below. Ideas on how to get it working or a better solution using standard libraries?

import decimal
import money
import sqlite3

sqlite3.register_adapter(decimal.Decimal, lambda x:str(x))
sqlite3.register_converter('decimal', decimal.Decimal)
sqlite3.register_adapter(money.Money, lambda x: int(value*100))
sqlite3.register_converter('intcents', lambda x: money.Money(x)/100)

conn = sqlite3.connect(":memory:",
        detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cursor = conn.cursor()
cursor.executescript("CREATE TABLE example(rate decimal, amount intcents)")

for x in (1,1,1,2,2,2,3,3,3):
    rate = decimal.Decimal(str(x))/100
    amount = money.Money(str(rate))

    try:
        cursor.execute("INSERT INTO example VALUES(?,?)", (rate, amount))
    except:
        print (rate, amount)
        raise

cursor.execute("""SELECT sum(rate), sum(amount) FROM example""")
print cursor.fetchone()
cursor.execute("""SELECT sum(rate) as "t [decimal]", sum(amount)as "a [intcents]"FROM example""")
print cursor.fetchone()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:24:00+00:00Added an answer on May 22, 2026 at 2:24 pm

    The problem was in the register_adapter. Odd, it didn’t cause give me a NameError. The other solution would require processing at every input and output. This would negate the reason for register_adapter and register_converter. With the syntax fix in the comment, it works quite well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using sqlite3 and Django I want to change to PostgreSQL and keep all data
I have a sqlite database that I want to open using sqlite3.exe. Now I
We are currently using SQLite3 as our database and want to switch it to
Iam coding in c and using sqlite3 as database .I want to ask that
I am able to create a connection to a local sqlite3 database ( Using
I am using an sqlite database for a producer-consumer queue. One or more producers
I'm building a database library for my application using sqlite3 as the base. I
I am using android build target vendor version 2.2. I want to access/read sqlite3
i am new to iPhone, I am using sqlite3 as my database. There are
I'm using Rails 3, and Ubuntu. I want to change the default Rails SQLite3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.