【发布时间】:2014-03-19 11:21:46
【问题描述】:
我正在使用 python 2.7.6 和 sqlite 3.3.6。
我有一个带有 INT 列的表。当我在列上选择max() 或min() 时,会返回该行中的一些随机数,它既不是最小值也不是最大值。我试图重现这种行为,手动创建表并插入相同的值,最小值和最大值完美地工作。但是,在我的脚本之后,手动查询表再次给出了奇怪的结果。
这里是创建表语句
CREATE TABLE customer_score ( DATE INT , SPAUBM INT , "CUSTOMER" TEXT, "SCORE" INT )
我使用 sqlite 中的 executemany 方法来填充表格。
cursor.executemany("INSERT INTO customer_score VALUES ( ?, ?, ? , ? )", list_to_put)
这是从文件放入表中的行示例
4203 6JASTYMPT 987335
问题示例:
sqlite> select * from customer_score where customer='AAA' and date in (20140219);
20140219|9214|AAA|5017262
20140219|9213|AAA|3409363
20140219|9207|AAA|2288238
20140219|9208|AAA|809365
sqlite> select max(score) from customer_score where customer='AAA' and date in (20140219);
809365
sqlite> select min(score) from customer_score where customer='AAAL' and date in (20140219);
2288238
可能是 executemany 方法在插入表之前以某种方式破坏了 INT 数据吗?
我不知道如何检查。
更新
SQlite 手册说 MIN() MAX() 是 ORDER BY LIMIT 1 的快捷方式。就我而言, ORDER BY 也无法按预期工作。行位置看起来完全随机。
sqlite> select * from customer_score where customer='AAA' and date in (20140218, 20140219) order by score desc ;
20140219|9208|AAA|809365
20140218|9208|AAA|629937
20140219|9214|AAA|5017262
20140218|9214|AAA|3911855
20140219|9213|AAA|3409363
20140219|9207|AAA|2288238
20140218|9213|AAA|2127092
20140218|9207|AAA|1489895
【问题讨论】: