使用python删除mysql语句

问题描述 投票:0回答:2

我是 MYSQL 新手,我面临着一个非常简单的 MYSQL 问题。我正在创建一个包含学生表的数据库,该表包含学生的姓名、ID(主键)。我需要根据用户选择的id(调用这个变量student_id)删除一条记录,那么如何使用python在mysql语句中编写这个?我已经尝试过,但我知道这是错误的 -->

cur.execute("Delete FROM students WHERE ID = student_id")
python mysql python-3.x
2个回答
2
投票

这应该适合你:

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

0
投票
@app.route('/delete', methods=['POST','GET'])
def delete():
if request.method == 'POST':
    flash("Data deleted Successfully")
    curobj = conn.cursor()
    id = int(input("enter the id:"))
    query = """DELETE FROM 'marks' WHERE id=%s"""
    curobj.execute(query,(id))
    conn.commit()
    

这段代码有什么错误?它不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.