python - 使用python删除mysql语句

标签 python mysql python-3.x

我是 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/

相关文章:

python - 将 DataFrame 的列设置为 pandas 中另一个列的总和

python - 将 np 矩阵及其索引转换为两个列表

mysql - 无法安装 mysql-server E : Sub-process/usr/bin/dpkg returned an error code 1

mysql存储过程执行准备语句不起作用

php - 从下拉列表中获取默认值并显示表格

Python:在匹配行之后从文件中提取3行

python - 在 Python 2 和 Python 3 中子类化内置类型

python - 如何检查一个值是否与 python 中的类型匹配?

python - 您无权使用 BeautifulSoup 访问网站

python - 在 Python3 中,当我读取二进制文件时,为什么 'b' 会添加到我的内容前面?