gpt4 book ai didi

python - 并非所有参数都在SQL语句中使用,sql语句中的变量

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:32 25 4
gpt4 key购买 nike

我正在尝试使用 SQL 为我的学校项目做一些事情。要在 SQL 语句中使用变量,我遵循了这篇文章:How to use variables in SQL statement in Python? 。但我在此函数中收到以下错误:

Something went wrong: Not all parameters were used in the SQL statement

def Einkauf():
Typ = int(input("Typ"))
Anzahl = input("Anzahl")
MHD = input("MHD (YYYYMMDD)")
try:
cursor.execute("insert into Warenlager(Anzahl, MHD, StatusID, TypId) values(?,?,1,?)", (Anzahl,MHD,Typ))
db.commit()
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))

最佳答案

正如我在评论中所说,mysql.connector 使用 %s 作为占位符。

cursor.execute("insert into Warenlager(Anzahl, MHD, StatusID, TypId) values(%s, %s, 1, %s)", (Anzahl,MHD,Typ))

请注意,这并非不安全,因为您没有将其用于字符串插值。这是不安全的:

cursor.execute("insert into Warenlager(Anzahl, MHD, StatusID, TypId) values(%s, %s, 1, %s)" % (Anzahl,MHD,Typ))  # don't do this

事实上,您引用的帖子说的是完全相同的事情,所以我不知道您从哪里得到不应该这样做的想法。

关于python - 并非所有参数都在SQL语句中使用,sql语句中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52983277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com