I want to delete the whole row when user enters the id. If it matches, I completely delete whole row from table.
@staticmethod
def close_account(value):
check()
rows = cur.fetchall()
for row in rows:
if rows is None:
print("Sorry It's an Empty.............. ")
return True
elif rows[0] == value:
del_row = "Delete From customer where id = value"
cur.execute(del_row)
return True
It shows error in del_row that:
value is unable to resolve column value
Even if I enter the id = 1 it also does not delete the row.
Considering your id
attribute is a string, change this line:
del_row = "Delete From customer where id = value"
to:
del_row = "Delete From customer where id = %s" % value
The way you have it, the database is trying to delete DELETE FROM customer WHERE id="value"
, whereas your goal is to replace value
with the function parameter (eg DELETE FROM customer WHERE id="123"
Disclaimer : This solution makes your code susceptible to SQL injection
There are a couple of problems here. Primarily you're not passing a value in for the id = X
condition in your query, so you'll need to do that - preferably using a parameterized query , that looks like this:
del_row = "DELETE FROM customer WHERE id = %s"
cur.execute(del_row, (1))
Your second problem is the assumption that the row ID is 1
, when this may not be the case. If your ID
field is auto incrementing, then it may be far greater than that. If your original query is returning the ID as a column, you should be able to retrieve the row ID using something like:
row[0]
Which, when passed into the first code block, means you end up with something like this:
@staticmethod
def close_account(value):
check()
rows = cur.fetchall()
for row in rows:
if rows is None:
print("Sorry It's an Empty.............. ")
return True
elif rows[0] == value:
del_row = "DELETE FROM customer WHERE id = %s"
cur.execute(del_row, row[0])
return True
"Delete From customer where id = %s" % value
will possibly get you what you want. On the other hand, it's a bad idea to programatically walk through all rows of the database to find a single one to delete, but I guess there are even bigger fish to fry first...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.