[英]Python dict into MySQL executemany
My statement should match syntax, but I get error "Not all parameters were used in the SQL statement") mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
我的语句应与语法匹配,但出现错误"Not all parameters were used in the SQL statement") mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
stmt = "UPDATE abbcards SET carPlate = '%(values)s' WHERE carId =%(id)s"
params = [tuple(cusSurname.values()), tuple(cusSurname.keys())]
cur.executemany(stmt, params)
What's wrong? 怎么了? I've defined the operation
and seq_params
variables. 我已经定义了operation
和seq_params
变量。
@ThePjot is somewhat right in their comment. @ThePjot在他们的评论中有些正确。 You are using named placeholders in your query string ( %(values)s
and %(id)s
), so you have to pass the parameters in the form of a dict. 您在查询字符串( %(values)s
和%(id)s
)中使用命名占位符,因此必须以字典形式传递参数。
BUT , you also call executemany
which needs a list or tuple of parameters . 但是 ,您还调用了executemany
,它需要一个参数列表或元组。 Many times, executemany
does nothing else than calling execute
multiple times while iterating over params
, passing an element of parameters in every iteration. 很多时候, executemany
除了在遍历params
同时多次调用execute
executemany
,什么都不做,在每次迭代中都传递参数元素。 That means, each element of params
must contain a full set of parameters for the query. 这意味着, params
每个元素必须包含用于查询的完整参数集。
Looking at your query string you need two parameters to be substituted. 查看您的查询字符串,您需要替换两个参数。
stmt = "UPDATE abbcards SET carPlate = '%(values)s' WHERE carId =%(id)s"
# Side note: the quote chars around %(values)s are quite certainly wrong here
From your code examples, one could assume that cusSurname
is a dict. 从您的代码示例中,您可以假定cusSurname
是一则字典。 Let's say it looks like this: 假设它看起来像这样:
cusSurname = {'1': 'a1b1c1',
'2': 'a2b2c2',
'3': 'a3b3c3'}
What you are doing in the code above, is passing all the keys and all the values of cusSurname to executemany
. 您在上面的代码中所做的就是将所有键和cusSurname的所有值传递给executemany
。
params = [('a1b1c1', 'a2b2c2', 'a3b3c3'), ('1', '2', '3')]
This would lead to a final query string of: 这将导致最终查询字符串为:
UPDATE abbcards SET carPlate = '\'a1b1c1\'' WHERE carId ='a2b2c2'
... with one element from the first parameter tuple not being substituted, which leads to mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
, before the second statement could be formed from the string and the params, which would look like: ...,第一个参数元组中的一个元素未被替换,这导致mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
,然后才能由字符串和参数形成第二个语句,看起来像:
UPDATE abbcards SET carPlate = '\'1\'' WHERE carId ='2'
... and raise the same exception. ...并提出相同的例外。 What you'll want to do, is end up with a list/tuple of dicts consisting of individual items of your cusSurname
: 您想要做的是最终显示由cusSurname
的各个项目组成的字典列表/元组:
stmt = "UPDATE abbcards SET carPlate = %(values)s WHERE carId =%(id)s"
params = [dict({'id': k, 'values': v}) for k,v in cusSurname.items()]
# [{'id': '1', 'values': 'a1b1c1'}, {'id': '2', 'values': 'a2b2c2'}, {'id': '3', 'values': 'a3b3c3'}]
cur.executemany(stmt, params)
Or switch to positional params: 或切换到位置参数:
stmt = "UPDATE abbcards SET carPlate = %s WHERE carId =%s"
params = [tuple(i) for i in cusSurname.items()]
# [('1', 'a1b1c1'), ('2', 'a2b2c2'), ('3', 'a3b3c3')]
cur.executemany(stmt, params)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.