gpt4 book ai didi

python - sqlite 不保存小数,但在 MySQL 中工作正常

转载 作者:行者123 更新时间:2023-11-29 14:15:39 30 4
gpt4 key购买 nike

Possible Duplicate:
How to convert Python decimal to SQLite numeric?

我遇到了 sqlite 不显示小数点后两位的值的问题,但是当我在 MySQL(工作台)上测试它时,它工作正常。我没有给出错误或任何东西,只是静静地消失。我想查出这个巫毒的真相,这样它就不会回来咬我了。

MY_CHOICE = (
(Decimal('20.00'), '20'), /#doesnt work with sqlite
(Decimal('20.00'), '20.00'), /#doesnt work with sqlite
(Decimal('10.50'), '10.50'), /#doesnt work with sqlite
(Decimal('5.05'), '5.05'), /#works with sqlite
(Decimal('4.75'), '4.75'), /#works with sqlite
)

my_model = models.DecimalField(max_digits=6, decimal_places=2, choices=My_CHOICE, null=True, blank=True)

任何可能有帮助的想法或想法都会很棒。谢谢。

最佳答案

SQLite 是非常“民主”的轻量级数据库。例如,您可以声明 VARCHAR(20) 字段并向其中写入 30 个符号的字符串,而不会出现任何错误或警告消息。

来自 SQLite documentation :

A string might look like a floating-point literal with a decimal point and/or exponent notation but as long as the value can be expressed as an integer, the NUMERIC affinity will convert it into an integer. Hence, the string '3.0e+5' is stored in a column with NUMERIC affinity as the integer 300000, not as the floating point value 300000.0.

关于python - sqlite 不保存小数,但在 MySQL 中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12748552/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com