使用sqlite3作为数据库,我想保留几个decimal.Decimal值,一个百分比另一个美元金额.但是我在超过20,000个条目上使用sum(金额)时遇到了问题.这是几美元.
我想过使用适配器来节省分数,然后聚合应该有效.
sqlite3.register_adapter(decimal.Decimal,lambda x:str(x)) sqlite3.register_adapter(decimal.Decimal,lambda x:int(x*100))
但是,我现在需要两个类,因为我不能使用同一个类.试图将Decimal子类化成一个问题因为它本身使用了Decimal.精细.我将复制decimal.py并用Money | money替换每个Decimal | decimal的出现.
$copy decimal.py money.py $sed -e "s/Decimal/Money/g" -e "s/decimal/money/g" -i money.py $money.py
所有单元测试都有效.我现在尝试,我得到一个“可能不受支持的类型”错误.我改变转换器,使用基本类型.我只是无法让它正常工作,我对这个问题没有更好的想法.
我需要一些帮助,下面有一个示例.关于如何使用标准库使其工作或更好的解决方案的想法?
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,2,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.转换为字符串并将字符串存储在DB中 2.乘以100并存储为货币值的整数 当从DB读取值时,这两个都需要转换回decimal.Decimal.