Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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列表_Python_Plugins - Fatal编程技术网

查询中的Python列表

查询中的Python列表,python,plugins,Python,Plugins,我正在使用python插件。我使用列表存储一些值,如下所示: known_stn.append('1') known_stn.append('2') 我的问题是 query=("SELECT survey, station FROM stat WHERE stat.station IN (%s) AND station.survey = '2011410'" %known_stn) 中的station.station(['1',2'])出现错误,因为列表包含[]个括号。 我试着换掉那些支架,

我正在使用python插件。我使用列表存储一些值,如下所示:

known_stn.append('1')
known_stn.append('2')
我的问题是

query=("SELECT survey, station FROM stat WHERE stat.station IN (%s) AND station.survey = '2011410'" %known_stn)
中的station.station(['1',2'])
出现错误,因为列表包含[]个括号。 我试着换掉那些支架,但没有换掉


是否有其他数据结构可供使用??或者替换方括号的方法…

您需要先将列表转换为字符串:

>>> my_list = [1,2,3]
>>> str(my_list)
'[1, 2, 3]'
>>> map(str, my_list)
['1', '2', '3']
>>> ','.join(map(str, my_list))
'1,2,3'
>>> 'select ... where foo in (%s)' % ','.join(map(str, my_list))
'select ... where foo in (1,2,3)'

您需要先将列表转换为字符串:

>>> my_list = [1,2,3]
>>> str(my_list)
'[1, 2, 3]'
>>> map(str, my_list)
['1', '2', '3']
>>> ','.join(map(str, my_list))
'1,2,3'
>>> 'select ... where foo in (%s)' % ','.join(map(str, my_list))
'select ... where foo in (1,2,3)'

在将列表替换为模板字符串之前,需要将其格式化为字符串:

"where (%s) blah" % ', '.join(map(str,known_stn))

map(str,known\u stn)
在加入之前将元素本身转换为字符串


另外,请注意有关SQL注入的警告。

在将列表替换为模板字符串之前,您需要将其格式化为字符串:

"where (%s) blah" % ', '.join(map(str,known_stn))

map(str,known\u stn)
在加入之前将元素本身转换为字符串


另外,请注意有关SQL注入的警告。

“Python插件”并不意味着什么。您使用什么与SQL接口?我使用postgresSQL作为数据库。pyqt4 designer for GUI…..不要自己格式化查询字符串;这要求进行SQL注入攻击。在执行查询时使用任何内置格式。(下面的答案是正确的,因为您需要先将列表加入到字符串中。)例如,在使用的任何模块中,您都应该编写
游标。执行(查询、参数)
“Python插件”并不意味着任何事情。您使用什么与SQL接口?我使用postgresSQL作为数据库。pyqt4 designer for GUI…..不要自己格式化查询字符串;这要求进行SQL注入攻击。在执行查询时使用任何内置格式。(下面的答案是正确的,因为您需要先将列表加入到字符串中。)例如,在使用的任何模块中,您都应该编写
游标。执行(查询,参数)