Python MYSQL 更新
Python MYSQL update
我正在使用下面的代码来更新我的 table 的记录,但它不起作用并且不会更新我的 table
conn = mysql.connector.connect(host='localhost',
database='rps',
user='root',
password='')
cur = conn.cursor()
cur.execute("""UPDATE players SET score=%s WHERE chat_id =%s""", (int(self.p1.score), str(self.p1.chat__id)))
cur.execute("""UPDATE players SET score=%s WHERE chat_id =%s""", (int(self.p2.score), str(self.p2.chat__id)))
conn.commit()
cur.close()
conn.close()
完全正确,但我不知道如何解决这个问题
你能帮我解决吗?
谢谢
您可以在查询执行时设置 try/catch:
try:
cursor.execute(some_statement)
except MySQLdb.IntegrityError, e:
# handle a specific error condition
except MySQLdb.Error, e:
# handle a generic error condition
except MySQLdb.Warning, e:
# handle warnings, if the cursor you're using raises them
如果你没有捕获到任何异常,你应该检查你的程序逻辑。
我正在使用下面的代码来更新我的 table 的记录,但它不起作用并且不会更新我的 table
conn = mysql.connector.connect(host='localhost',
database='rps',
user='root',
password='')
cur = conn.cursor()
cur.execute("""UPDATE players SET score=%s WHERE chat_id =%s""", (int(self.p1.score), str(self.p1.chat__id)))
cur.execute("""UPDATE players SET score=%s WHERE chat_id =%s""", (int(self.p2.score), str(self.p2.chat__id)))
conn.commit()
cur.close()
conn.close()
完全正确,但我不知道如何解决这个问题 你能帮我解决吗? 谢谢
您可以在查询执行时设置 try/catch:
try:
cursor.execute(some_statement)
except MySQLdb.IntegrityError, e:
# handle a specific error condition
except MySQLdb.Error, e:
# handle a generic error condition
except MySQLdb.Warning, e:
# handle warnings, if the cursor you're using raises them
如果你没有捕获到任何异常,你应该检查你的程序逻辑。