1

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")
3
  • What about it makes you say it is wrong? What library are you using to communicate with your database? Commented Dec 4, 2016 at 17:42
  • After running query, you need to commit the changes using con.commit() where con is your connection object Commented Dec 4, 2016 at 17:44
  • @ Scot Hunter i have used this import mysql.connector , if that what you mean . @MoinuddinQuadri I have done so Commented Dec 4, 2016 at 18:36

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you but it didn't worked for me , here is my code for deleting the record : student_id=input('please enter ID') statmt="DELETE FROM student WHERE id = ?" cur.execute(statmt,(student_id,)) conn.commit()
Note the table name is student. Here is the error :Traceback (most recent call last): File "C:/Users/Ahmed/PycharmProjects/DATABASE/Test2.py", line 42, in <module> cur.execute(statmt,(student_id,)) File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 507, in execute "Not all parameters were used in the SQL statement") mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
You use which version of Python?
my python version is 3.4.0
So you need to use student_id = int(input('please enter ID')). Take a look at my edited answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.