如何为“ IN子句”传递变量参数

我正在准备如下的session.execute语句.
我几乎没有条件,只有一个IN子句.我收到以下错误.
我知道我做错了,但无法使它起作用.
filter_site_value = [‘过滤器1′,’过滤器2’]

session = get_session()
query = 'SELECT * FROM table where cv = %s AND dt > %s and dt < %s AND st IN  (%s)' % ','.join('%s' for i in filter_site_value)
data = (filter_customer_value,filter_date_start_value, filter_date_end_value, filter_site_value)
rows = session.execute(query, data)

“错误类型”:“类型错误”,
  “的errorMessage”:“没有足够的论据格式字符串”

请帮忙.

解决方法:

您的字符串在3个地方需要格式化一个值(%s),您只提供一个值:’,’.join(‘%s’在filter_site_value中为i

因此,如果您有:

x = string_to_format % values

并且string_to_format包含X个%s(或%d%r …),则您需要在值中包含X个值

看到:
https://docs.python.org/2/library/string.html,https://pyformat.info/

您可能想做的是:

query = 'SELECT * FROM table where cv = %s AND dt > %s and dt < %s AND st IN  ('+ ','.join(filter_site_value)+')'
data = (filter_customer_value,filter_date_start_value, filter_date_end_value)

要么

query = 'SELECT * FROM table where cv = %s AND dt > %s and dt < %s AND st IN  ('+ ','.join(%s for i in filter_site_value)+')'
data = (filter_customer_value,filter_date_start_value, filter_date_end_value)+tuple(filter_site_value)
上一篇:无法使用DataStax Python驱动程序远程连接到Cassandra


下一篇:连接Cassandra DB时java.lang.NoClassDefFoundError:ch / qos / logback / core / joran / spi / JoranExcepti