我是 MYSQL 的新手,我正面临一个非常简单的 MYSQL 问题。我正在创建一个包含学生表的数据库,该表包含学生的姓名、ID(主键)。我需要根据用户选择的 id 删除一条记录(调用这个变量 student_id),那么如何使用 python 在 mysql 语句中写这个?我试过了,但我知道这是错误的 -->
cur.execute("Delete FROM students WHERE ID = student_id")
最佳答案
这应该适合你:
student_id = int(input('Please, enter an ID: ')) # In Python 3, you need to parse the user input for numbers.
statmt = "DELETE FROM `students` WHERE id = %s"
cur.execute(statmt, (student_id,))
conn.commit() # You need to commit the transaction
关于python - 使用python删除mysql语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961557/