2014-03-19 20 views
1

我使用python 2.7.6與sqlite 3.3.6。 我有一個INT列的表。當我在列上選擇max()min()時,將返回行中的一些隨機數,既不是最小值也不是最大值。我試圖重現這種行爲,手動創建表並插入相同的值,最小和最大工作完美。但是,在我的腳本之後,手動查詢表再次給出奇怪的結果。奇怪的最大分鐘結果爲int列

這裏是create table語句

CREATE TABLE customer_score (DATE INT , SPAUBM INT , "CUSTOMER" TEXT, "SCORE" INT) 

我用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數據在插入表之前以某種方式?

我不知道該如何檢查。

UPDATE

SQlite的手冊上說MIN()MAX()被shorcut到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 

回答

3

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

您必須確保list_to_put中的值實際上是數字。

(請注意,SQLite的幾乎ignores the column data type;這不要緊,你是否聲明爲INTTEXTFLUFFY BUNNIES。)

+0

謝謝!我錯過了檢查這個。我確信,如果我將字符串形式的數字以sql格式提供給INT格式的列,它將被自動轉換爲INT或拋出異常。關於日期,只要格式是YYYYMMDD,我可以將日期作爲數字進行比較,這就是爲什麼我將它們保留在INT中。 –