Python MYSQL升级版

2024-04-29 02:37:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码来更新我的表的记录,但它不起作用,也不更新我的表

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()

这是完全正确的,但我不知道如何解决这个问题 你们能帮我解决吗伙计们? 谢谢


Tags: selfidexecutechatupdateconnwhereint
1条回答
网友
1楼 · 发布于 2024-04-29 02:37:47

您可以在查询执行时设置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

如果你没有捕捉到任何异常,你应该检查你的程序逻辑。在

相关问题 更多 >