【问题标题】:Python SQlite ORDER BY command doesn't workPython SQlite ORDER BY 命令不起作用
【发布时间】:2013-04-24 22:17:49
【问题描述】:

我刚开始编程,并使用 sqlite3 在 pyscripter 中编写了几行代码。

表“gather”是预先创建的。然后我从“收集”中选择某些行以将它们放入另一个表中。我尝试按特定列“日期”对该表进行排序。但这似乎不起作用。它不会给我错误消息或类似的东西。它只是没有排序。如果我在 sqlitemanager 中尝试相同的命令(SELECT * FROM 匹配 ORDER BY 日期),它在完全相同的表上工作正常!这里有什么问题?我用谷歌搜索了一段时间,但我没有找到解决方案。这可能是我想念的愚蠢的东西..

正如我所说,我完全是新手。我猜你们看到代码都会流泪。因此,如果您有任何提示我可以如何缩短代码或使其更快或其他什么,非常欢迎您:) (但除了上述部分之外,一切都很好。)

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()

【问题讨论】:

  • 我有点不清楚到底是什么问题。是 INSERT 工作但 SELECT * FROM 匹配 ORDER BY 日期不是?
  • 您实际上是在填充日期列吗?我没看到。
  • 它也将它作为字符串排序,而不是按日期排序,因为您指定它是 TEXT ...。在您选择之后,您还需要 results = cursor1.fetchall() ...
  • @deakolt 问题是命令“SELECT * FROM 匹配 ORDER BY date”在 sqlitemanager 中有效,但如果我在 pyscripter 中运行它则不起作用。
  • @JamesThiele 是的,我确实在日期列中填充了“插入匹配(日期、团队 1、团队 2)SELECT * FROM 收集 WHERE 团队 1=?或团队 2=?,(a,a,)”

标签: python sql sqlite sql-order-by


【解决方案1】:

好的,我想我明白你的问题了。首先:我不确定该提交调用是否有必要。但是,如果是,您肯定希望它出现在您的 select 语句之前。 'connection.commit()' 本质上是说,提交我刚刚对数据库所做的更改。

您的第二个问题是您正在执行选择查询,但实际上从未对查询结果执行任何操作。

试试这个:

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

【讨论】:

  • 行得通。坦克。但是我怎样才能将这个新订单保存到数据库本身呢?
  • 在下面查看我的编辑。订单基本上不能保存到数据库,但是可以按顺序创建。
【解决方案2】:

尝试将commit 移动到SELECT * 之前(我不确定100% 这是一个问题)然后您只需要获取查询结果:-) 在之后添加类似res = cursor1.fetchall() 的行你有executed 和SELECT。如果您想像在 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,这就是我的想法。

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2014-08-03
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多