SQLite查询从lis中选择号码

2024-04-24 08:15:09 发布

您现在位置:Python中文网/ 问答频道 /正文

当您想在Python中编写一个查询,该查询将选择(从SQLite数据库)所有有300页的书籍,您将编写:

numsPages = 300
cur.execute("select * from  books where number_of_pages = :numsPages", locals())

问题是当你想从某一组中选择有页数的书时

^{pr2}$

上述说法是不可能的。在

很难在语句中编写许多or运算符,因此我宁愿使用in运算符。在

在不使用许多or运算符和使用一些Python列表或其他数据结构的情况下,如何编写查询?在

也许,这个例子看起来不太实际(事实也是如此),但是当我用这个例子的时候,这个问题就更容易理解了。在


Tags: oroffrom数据库numberexecutesqlite运算符
2条回答

您可以自己设置语句格式,例如:

a = [100, 200, 300, 400]
## The a bit more formatting is used because the list contains integers, i.e., the join with string casting
stmt = 'select * from books where number_of_pages in ({0})'.format(', '.join([str(v) for v in a]))

print stmt
>>> select * from books where number_of_pages in (100, 200, 300, 400)

您遇到的问题有一个很好的分类,您可以在这里找到解决方案:

http://www.javaranch.com/journal/200510/Journal200510.jsp#a2

这是从:

https://stackoverflow.com/a/189399/1232478

相关问题 更多 >