S
S
Studentka19962020-02-06 13:25:54
Python
Studentka1996, 2020-02-06 13:25:54

How to delete the last record in a table?

Hello. What is wrong? I want to delete the last record in the Customer table and the Delivery table

cur.execute('DELETE FROM Клиент ORDER BY Код_клиента DESC LIMIT 1')
        cur.execute('DELETE FROM Доставка ORDER BY Код_доставки DESC LIMIT 1')
        con.commit() #сохранить изменения

5e3be9a014aa6553813692.png

Option 1 worked:
cur.execute('SELECT Код_доставки FROM Доставка ORDER BY Код_доставки DESC LIMIT 1')
        show = cur.fetchone()
        print(show[0])
        cur.execute(f'DELETE FROM Доставка WHERE Код_доставки = {show[0]}')
        con.commit() #сохранить изменения

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-02-06
@Studentka1996

First, the delete statement does not accept order by clauses. Secondly, SQLite, like many other DBMS, stores data in an arbitrary order, and therefore the concept of "last record" does not make sense. You need to make a selection sorted by the field that determines the order of the records, get the ID of the first or last record in the selection and pass it to the WHERE clause of the delete query.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question