1

我有一个名为“ critical_severity_list”的列表,如下所示:[30, 40]

我想执行一个带有 IN 子句的选择,如下所示:

cursor = connection.cursor()

que = 'select * from tm_txn_age_severity_config where txn_state_id = %s and txn_type_id = %s and txn_age_severity_id in ( %s)'

in_ids = ', '.join(map(lambda x: '%s', critical_severity_list))

que = que % ('%s', in_ids)

bind_var_list = [] 

bind_var_list.append(rec[1])

bind_var_list.append(rec[2])

bind_var_list.extend(critical_severity_list)

cursor.execute(que, bind_var_list)

result = cursor.fetchall()

我收到此错误:格式字符串的参数不足

有人可以帮我解决....??

4

2 回答 2

0

我希望这是你想要的:

critical_severity_list=[30,40]
temp=['%s']+critical_severity_list
que = 'select * from tm_txn_age_severity_config where txn_state_id = %s and txn_type_id = %s and txn_age_severity_id in ( %s)'
que = que % tuple(i for i in temp)
print(que)

输出:

select * from tm_txn_age_severity_config where txn_state_id = %s and txn_type_id = 30 and txn_age_severity_id in ( 40)
于 2012-06-19T06:28:27.820 回答
0

错误发生在这里:

que = 'select * from tm_txn_age_severity_config where txn_state_id = %s and txn_type_id = %s and txn_age_severity_id in ( %s)'
in_ids = ', '.join(map(lambda x: '%s', critical_severity_list))
que = que % ('%s', in_ids)

您正在que使用多个参数进行格式化,但您没有提供足够的参数。其中que出现了 3 次,%s但是当您使用%运算符格式化字符串时,您只提供了两个参数。

例如:

>>> "%s %s" % "a"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

(提供了一个参数,预计两个)

于 2012-06-19T06:26:55.383 回答