2024-06-02 05:32:26 发布
网友
我正在尝试更正Python MYSQL update语句(使用变量):
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))
但是{a2}。因为这样做很简单(也很相似),所以要正确地进行操作。正确地做。
唯一需要注意的是,某些数据库后端对于字符串替换不遵循相同的约定(想到SQLite)。
你的语法全错了:
更多信息,read the documentation。
以下是正确的方法:
import MySQLdb if __name__ == '__main__': connect = MySQLdb.connect(host="localhost", port=3306, user="xxx", passwd="xxx", db='xxx', charset='utf8') cursor = connect.cursor() cursor.execute(""" UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server=%s """, (Year, Month, Day, Hour, Minute, ServerID)) connect.commit() connect.close()
p.S.别忘了connect.commit(),否则不行
connect.commit()
它should be:
你也可以用基本的字符串操作来完成
但是{a2}。因为这样做很简单(也很相似),所以要正确地进行操作。正确地做。
唯一需要注意的是,某些数据库后端对于字符串替换不遵循相同的约定(想到SQLite)。
你的语法全错了:
更多信息,read the documentation。
以下是正确的方法:
p.S.别忘了
connect.commit()
,否则不行相关问题 更多 >
编程相关推荐