Python:奇怪的SQLite3查询结果

时间:2013-10-07 19:34:06

标签: python python-3.x sqlite

我在Python3中使用SQLite3查询得到了意想不到的结果。

if first_waypoint_number < last_waypoint_number:
        result = list(c.execute("SELECT WPNumber, WPID, ROUTE FROM rte WHERE ROUTE = ? AND WPNumber BETWEEN ? AND ?", (route, first_waypoint_number, last_waypoint_number)))
        print(result)
else:
        result = list(c.execute("SELECT WPNumber, WPID, ROUTE FROM rte WHERE ROUTE = ? AND WPNumber BETWEEN ? AND ? ORDER BY WPNumber DESC", (route, first_waypoint_number, last_waypoint_number)))
        print(result)

第一个查询的工作方式与预期相同,并产生以下结果:

[('A123', 25, 'WAYPOINTX'), ('A123', 26, 'WAYPONTY'), ('A123', 27, 'WAYPOINTZ')]

然而,第二个查询产生一个空列表[],而它应该以相反的顺序返回相同的列表。

我已经在SQLite3中测试了两个查询而没有任何问题。

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:2)

The sqlite BETWEEN operator is equivalent to x>=y AND x<=z,因此在y > z时没有匹配。

sqlite> create table foo (x int);
sqlite> insert into foo values (1);
sqlite> insert into foo values (2);
sqlite> insert into foo values (3);
sqlite> insert into foo values (4);
sqlite> select * from foo;
1
2
3
4
sqlite> select * from foo where x between 2 and 3;
2
3
sqlite> select * from foo where x between 3 and 2;
sqlite>