使用适配器在sqlite3中使用十进制和金钱的问题
发布时间:2020-12-12 19:00:48 所属栏目:百科 来源:网络整理
导读:使用sqlite3作为数据库,我想保留几个decimal.Decimal值,一个百分比另一个美元金额.但是我在超过20,000个条目上使用sum(金额)时遇到了问题.这是几美元. 我想过使用适配器来节省分数,然后聚合应该有效. sqlite3.register_adapter(decimal.Decimal,lambda x:str
使用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.(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |