我试图通过将用户输入除以100,在sqlite3数据库中将百分比值存储为小数。
但是,在数据库中,如果列类型为INTEGER,则存储的值始终为0;如果类型为REAL,则存储的值始终为0.0。
我尝试更改数据类型。
sql = """INSERT INTO recipes (rname,customer_id,bar_weight,ingredient_id,ingredient_amount) VALUES (?,?,?/100)"""
预期:20/100 = 0.2
实际:0
我试图通过将用户输入除以100,在sqlite3数据库中将百分比值存储为小数。
但是,在数据库中,如果列类型为INTEGER,则存储的值始终为0;如果类型为REAL,则存储的值始终为0.0。
我尝试更改数据类型。
sql = """INSERT INTO recipes (rname,customer_id,bar_weight,ingredient_id,ingredient_amount) VALUES (?,?,?/100)"""
预期:20/100 = 0.2
实际:0
此语句的结果:
select 20 / 100
是0
,因为SQLite会进行整数除法。
为避免这种情况,请改用100.0
:
INSERT INTO recipes (rname,customer_id,bar_weight,ingredient_id,ingredient_amount)
VALUES (?,?,?/100.0)