0

I have a database table called Students and I want to delete a record using SQL. Here is my code:

uid = int(input("Please enter students ID: "))
c.execute("DELETE FROM Students WHERE ID = (uid) ")

I want to input the ID variable (uid) into the c.execute

Thanks in advance.

icc97
  • 11,395
  • 8
  • 76
  • 90
G_man
  • 323
  • 2
  • 3
  • 6

3 Answers3

3

You must not use string interpolation as recommended in the other answer; while in this specific case it might be OK, generally it is unsafe as it opens you up to SQL injection. Instead, use the support for parameters in the execute method:

uid = int(input("Please enter students ID: "))
c.execute("DELETE FROM Students WHERE ID = %s", (uid,))
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

What Daniel Roseman said should be the correct answer.

You can insert the ID as a parameter for the .execute method. There is an answer about this here

Community
  • 1
  • 1
EyfI
  • 975
  • 2
  • 17
  • 24
0

Basically the syntax is:

"some string: %s, some int: %i, some double: %d" % (string_var,int_var,double_var)

so:

uid = int(input("Please enter students ID: "))
c.execute("DELETE FROM Students WHERE ID = %i" % (uid))
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91