Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 100个特定姓氏的返回记录_Python_Sqlite - Fatal编程技术网

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)