Fetch the last row of a cursor with SQLite

cheese1756 picture cheese1756 · May 19, 2014 · Viewed 11.6k times · Source

On Android, I am very used to using cursor.moveToLast() to fetch the last item in a cursor. I can't seem to find an equivalent for Python's SQLite, however. Is there a function which will allow me to get the last row of a cursor?

I could just call cursor.fetchall() and get the last item in the list, but is there something more efficient than that? Thanks.

Answer

PrivateUser picture PrivateUser · May 19, 2014
cursor.execute("SELECT * FROM table ORDER BY id DESC LIMIT 1")
result = cursor.fetchone()