Python 100个特定姓氏的返回记录
我有一个元组,Python 100个特定姓氏的返回记录,python,sqlite,Python,Sqlite,我有一个元组,l,有100个姓氏。如何在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中包含的姓氏的所有记录
l
,有100个姓氏。如何在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
中包含的姓氏的所有记录
问题:返回元组中包含的姓氏的所有记录
核心是,创建一个具有与序列中相同数量的绑定的查询-?
。需要使用
[:-1]
来排除最后一个逗号…?,
用Python:3.5.3-sqlite3:2.6.0测试过,不起作用?
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)