2013-04-24 239 views
1

我剛剛開始編程,並使用sqlite3在pyscripter中編寫了幾行代碼。Python SQlite ORDER BY命令不起作用

預先創建表格「聚集」。然後我從「聚集」中選擇某些行,將它們放到另一個表中。我嘗試按特定列「日期」對此表進行排序。但它似乎並不奏效。它不會給我一個錯誤信息或類似的東西。它只是沒有排序。如果我在sqlitemanager中嘗試相同的命令(SELECT * FROM匹配ORDER BY日期),它在完全相同的表上工作正常!這裏有什麼問題?我GOOGLE了一段時間,但我沒有找到解決辦法。它是可能的東西愚蠢我失蹤..

正如我所說我是一個總新手。我想你們都會在看代碼時流淚。所以,如果你有什麼祕訣,我怎麼可以縮短代碼或使其更快或什麼的,你是非常歡迎的。(但一切工作正常,除了上面提到的部分。)

import sqlite3 
connection = sqlite3.connect("gather.sqlite") 
cursor1 = connection.cursor() 
cursor1.execute('Drop table IF EXISTS matches') 
cursor1.execute('CREATE TABLE matches(date TEXT, team1 TEXT, team2 TEXT)') 
cursor1.execute('INSERT INTO matches (date, team1, team2) SELECT * FROM gather WHERE team1=? or team2=?, (a,a,)) 

cursor1.execute("SELECT * FROM matches ORDER BY date") 


connection.commit() 
+1

這是一個有點不清楚我到底是什麼問題。這是INSERT正在工作,但SELECT * FROM匹配ORDER BY日期不是? – deakolt 2013-04-24 22:20:21

+1

你實際上是在填充日期列嗎?我沒看到它。 – 2013-04-24 22:34:05

+0

它也會將它排序爲字符串,而不是按日期排序,因爲您指定它是TEXT ....在您做出選擇之後,您還需要'results = cursor1.fetchall()'... – 2013-04-24 22:41:24

回答

0

好的,我想我理解你的問題。首先:我不確定這個提交調用是否有必要。但是,如果是這樣,您一定希望它在選擇語句之前。 'connection.commit()'實質上是說,提交我剛剛對數據庫所做的更改。

你的第二個問題是你正在執行select查詢,但從來沒有對查詢的結果做任何事情。

試試這個:

import sqlite3 
connection = sqlite3.connect("gather.sqlite") 
cursor1 = connection.cursor() 
cursor1.execute('Drop table IF EXISTS matches') 
cursor1.execute('CREATE TABLE matches(date TEXT, team1 TEXT, team2 TEXT)') 
cursor1.execute('INSERT INTO matches (date, team1, team2) SELECT * FROM gather WHERE team1=? or team2=?, (a,a,)) 

connection.commit() 

# directly iterate over the results of the query: 
for row in cursor1.execute("SELECT * FROM matches ORDER BY date"): 
    print row 

您正在執行的查詢,但從來沒有真正檢索結果。有兩種方法可以用sqlite3來做到這一點:一種方法是我上面展示給你的方式,你可以直接使用execute語句作爲可迭代對象。

另一種方法是如下:

import sqlite3 
connection = sqlite3.connect("gather.sqlite") 
cursor1 = connection.cursor() 
cursor1.execute('Drop table IF EXISTS matches') 
cursor1.execute('CREATE TABLE matches(date TEXT, team1 TEXT, team2 TEXT)') 
cursor1.execute('INSERT INTO matches (date, team1, team2) SELECT * FROM gather WHERE team1=? or team2=?, (a,a,)) 

connection.commit() 

cursor1.execute("SELECT * FROM matches ORDER BY date") 

# fetch all means fetch all rows from the last query. here you put the rows 
# into their own result object instead of directly iterating over them. 
db_result = cursor1.fetchall() 
for row in db_result: 
    print row 
+0

工作。坦克。但我怎樣才能將它保存到數據庫本身? – user2317500 2013-04-25 08:25:00

+0

看到我的編輯如下。您基本上不能將訂單保存到數據庫,但您可以按順序創建它。 – Felipe 2013-04-25 14:13:53

0

嘗試移動commitSELECT *(我不能確定100%這是一個問題)然後您只需要獲取查詢結果:-)在executeSELECT之後添加一行,如res = cursor1.fetchall()。如果你想在sqlitemanager中顯示它們,請在底部添加

for hit in res: 
    print '|'.join(hit) 

編輯:爲了解決您的排序順序存儲表中的問題:

我想你在找什麼東西像一個聚集索引。 (它實際上並未對錶中的值進行排序,但接近;請參閱here)。

SQLIte沒有這樣的索引,但可以通過實際排序表來模擬它們。你只能做一次,因爲你插入的數據。你需要一個像下面這樣的SQL命令:

INSERT INTO matches (date, team1, team2) 
    SELECT * FROM gather 
    WHERE team1=? or team2=? 
    ORDER BY date; 

而不是你當前使用的那個。

請參閱點4 here,這是我的想法。

+0

也適用。非常感謝! – user2317500 2013-04-25 08:25:18