gpt4 book ai didi

Python MYSQL 更新语句

转载 作者:IT老高 更新时间:2023-10-28 12:59:12 26 4
gpt4 key购买 nike

我正在尝试使这个 Python MYSQL 更新语句正确(带变量):

cursor.execute ("UPDATE tblTableName SET Year=%s" % Year ", Month=%s" % Month ", Day=%s" % Day ", Hour=%s" % Hour ", Minute=%s" Minute "WHERE Server=%s " % ServerID)   

任何想法我哪里出错了?

最佳答案

should be :

cursor.execute ("""
UPDATE tblTableName
SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s
WHERE Server=%s
""", (Year, Month, Day, Hour, Minute, ServerID))

也可以通过基本的字符串操作来做到这一点,

cursor.execute ("UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server='%s' " % (Year, Month, Day, Hour, Minute, ServerID))

但是 this way is discouraged because it leaves you open for SQL Injection 。因为以正确的方式tm 这样做非常容易(并且类似)。正确地做。

唯一需要注意的是,一些数据库后端不遵循相同的字符串替换约定(想到 SQLite)。

关于Python MYSQL 更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1307378/

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