Joo
Joo

Reputation: 107

DELETE mysql statement using python

I am new to MYSQL and i am facing a very easy problem with MYSQL. I am creating a database that contains a student table , this table contains the student's name , ID( primary key) . I need to delete a record based on the user's choice of id ( call this variable student_id) , so how to write this in a mysql statement using python ? i have tried this but i know it is wrong -->

cur.execute("Delete FROM students WHERE ID = student_id")

Upvotes: 1

Views: 4089

Answers (1)

ettanany
ettanany

Reputation: 19806

This should work for you:

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

Upvotes: 2

Related Questions