S
S
Studentka19962020-02-01 21:49:51
SQL
Studentka1996, 2020-02-01 21:49:51

Why does deleting one record from the database not work?

What is wrong in the code? You need to remove the selected product from the cart. In the sql query, if I write a specific product, then yes, the deletion is performed, but you need to delete the product that is selected from the basket. Where is the mistake? Is the solution wrong? Who fumbles, tell me how to do it right ... Thank you!

cur.execute(f"SELECT * FROM Корзина_юзера WHERE Юзер = '{user}'")
    show_Korzina = cur.fetchall()
    print(show_Korzina)
    for row in show_Korzina:
        print(row[0])
        if text == 'Удалить_товар':
            if row[0] in show_Korzina:
                #Удалить выбранный товар
                cur.execute(f"DELETE FROM Корзина_юзера WHERE Товар = '{row[0]}' ")   
                print ( "Выбраный товар удалён из корзины")
                bot.send_message(message.chat.id, f'Товар  удалён из корзины') 
                con.commit() #сохранить изменения


As I understand it, it returns an empty list.
5e35c90be0960700465376.png

If we remove the line if row[0] in show_Korzina: then all products are deleted, how to write a condition for element ownership...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexkil, 2020-02-03
@alexkil

What is the composition of the fields in the table Cart_user ? On the screen in row[0] = Nikitich , is that a user?
Here it is more correct to write:
cur.execute(f"SELECT Product_ID , * FROM User_Cart WHERE User = '{user}'")
then it will work:
cur.execute(f"DELETE FROM User_Cart WHERE Product = '{row[0]} '")

K
Konstantin Tsvetkov, 2020-02-01
@tsklab

DELETE FROM Корзина_юзера WHERE Товар = '{row[0]}' AND Юзер = '{user}'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question