2013-04-24 31 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