插入数据库 MySQL 时出错"not all arguments converted during string formatting"



我有一个SQL查询,它给出了一个错误:

cur.execute("INSERT INTO `DB` (`ban`, `dntr`, `usrnm`, `id`, `dis`)  VALUES (1,0,?,?,?)",(param1,param2,param3,))

我不想在查询中使用%s,因为它容易进行SQL注入,并且我正在接受用户的输入。

mysqlclient使用%s作为占位符(请参阅文档中的示例(。

将代码更改为以下内容:

cur.execute("INSERT INTO `DB` (`ban`, `dntr`, `usrnm`, `id`, `dis`)  VALUES (1,0,%s,%s,%s)", (param1,param2,param3,))

你担心SQL注入是对的,但以上是可以的。您仍在使用带有参数的execute,因此它们将被转义。

你不应该做的事情是cur.execute(query % parameters, []).这容易受到 SQL 注入的影响。

相关内容

最新更新