简体   繁体   中英

Python: strange SQLite3 query result

I'm getting unexpected results with an SQLite3 query in Python3.

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)

The first query works like expected, and produces this result:

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

The second query however, produces an empty list [] , while it should return the same list in reverse order.

I've tested both queries in SQLite3 without problems.

Am I missing something here?

The sqlite BETWEEN operator is equivalent to x>=y AND x<=z , so it matches nothing when 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM