Django字典参数问题



我正在尝试在调用cursor.execute时使用Django字典参数,该参数记录在这个链接中

我有这个测试代码:

with connection.cursor() as cursor:
params = {"server_id": 1}
cursor.execute(
("select * from General.Servers where ServerID = %(server_id)s"), params,
)

但我得到了以下错误:

TypeError: format requires a mapping

这个错误是什么意思?以及如何修复?

谢谢!

您的代码有一个小错误。你必须像这样用%替换,

with connection.cursor() as cursor:
params = {"server_id": 1}
cursor.execute("select * from General.Servers where ServerID = %(server_id)s" %params)

同时移动括号内的params。

尝试:

with connection.cursor() as cursor:
params = {"server_id": 1}
cursor.execute(
("select * from General.Servers where ServerID = %s"), [params],
)

最新更新