返回 100 个特定姓氏的记录 [英] Return records for 100 specific surnames
本文介绍了返回 100 个特定姓氏的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个元组,l
,有 100 个姓氏.我怎样才能在 sqlite3 中做这样的事情:
I have a tuple, l
, with 100 surnames. How can I do something like this in sqlite3:
l = ("Smith", "Murphy", "Owens", ...)
with sqlite3.connect("census.sqlite") as conn:
c = conn.cursor()
c.execute('select firstname, surname from census_data where surname in ?',(l,))
这样我就可以返回l
中包含的姓氏的所有记录.
so that I can return all the records for the surnames contained in l
.
推荐答案
问题:返回
元组
核心是,创建一个 Query 与尽可能多的绑定 - ?
- 按照顺序.
需要 [:-1]
来排除最后一个逗号 ...?,
.
The core is, create a Query with as much bindings - ?
- as in the sequence.
The [:-1]
is needed to exclude the last comma ...?,
.
surnames = ("Smith", "Murphy", "Owens")
bindings = '?,'*len(surnames)
QUERY = "select firstname, surname from census_data where surname in ({});"
.format(bindings[:-1])
print(QUERY)
# >>> select firstname, surname from census_data where surname in (?,?,?);
cur.execute (QUERY, surnames)
使用 Python 测试:3.5.3 - sqlite3:2.6.0
这篇关于返回 100 个特定姓氏的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文