2

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.

cheese1756
  • 1,719
  • 3
  • 17
  • 25

1 Answers1

11
cursor.execute("SELECT * FROM table ORDER BY id DESC LIMIT 1")
result = cursor.fetchone()
PrivateUser
  • 4,474
  • 12
  • 61
  • 94
  • This works well. If you haven't defined a column that can be used, you can use the default undeclared column as the id. http://www.sqlite.org/c3ref/last_insert_rowid.html – Karr Oct 16 '19 at 22:17