我有一个名为" 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()
我收到此错误:格式字符串的参数不足
有人可以帮助我解决问题吗....??
错误发生在这里:
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
中,%s
出现三次,但在使用 %
运算符设置字符串格式时,仅提供两个参数。
例如:
>>> "%s %s" % "a"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
(提供一个参数,预期两个参数)
我希望这就是你想要的:
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)