【问题标题】:Weird max min results for int columnint 列的奇怪的最大最小值结果
【发布时间】: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

【问题讨论】:

    标签: python sqlite


    【解决方案1】:

    score 列中的值不是数字而是字符串。

    您必须确保list_to_put 中的值实际上是数字。

    (请注意,SQLite 几乎是ignores the column data type;无论您将其声明为INT 还是TEXTFLUFFY BUNNIES。)

    【讨论】:

    • 谢谢!我错过了检查这个。我确信如果我以字符串形式将带有数字的 sqlite 提供给 INT 格式的列,它将自动转换为 INT 或抛出异常。关于日期,只要格式为 YYYYMMDD,我就可以将日期作为数字进行比较,这就是我将它们保存在 INT 中的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2015-03-09
    相关资源
    最近更新 更多