Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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将列表元素分发到sql查询的一部分中_Python_Mysql_Iterable Unpacking - Fatal编程技术网

python将列表元素分发到sql查询的一部分中

python将列表元素分发到sql查询的一部分中,python,mysql,iterable-unpacking,Python,Mysql,Iterable Unpacking,在python中,我有以下sql查询: qry = "insert into golden_table (alpha, beta, week1, week2, week3, clust_list, nGram) values (%s %s %s %s %s %s %s)" 现在我想做一些类似的事情: L = ['a', 'b', 'c'] qry % (1, 2, UNPACK(L), "herp", "derp") 结果将是: "insert into golden_table (alph

在python中,我有以下sql查询:

qry = "insert into golden_table (alpha, beta, week1, week2, week3, clust_list, nGram) values (%s %s %s %s %s %s %s)"
现在我想做一些类似的事情:

L = ['a', 'b', 'c']
qry % (1, 2, UNPACK(L), "herp", "derp")
结果将是:

"insert into golden_table (alpha, beta, week1, week2, week3, clust_list, nGram) values (1, 2, 'a', 'b', 'c', "herp", "derp")"

如何实现这一点?

如果要构建元组,可以使用
tuple(L)
将列表转换为元组,并将元组与
+
组合

例:

>>> L
['a', 'b', 'c']
>>> (1,) + (2,) + tuple(L) + ("herp", "derp")
(1, 2, 'a', 'b', 'c', 'herp', 'derp')