1

I am using tinyDB to store some data with python, and I am trying to get the last row that I inserted to the db. so I am doing this:

data = db.all() # get all data from db
last_row = data[len(data) - 1]

and this is how I get the last row, but it is not so efficient because I am getting all the db at the beginning.

is there a better way doing so?

TheDragoner
  • 273
  • 2
  • 6
  • 17
  • Why do you want to get the "last row"? – Tomalak Mar 23 '18 at 11:46
  • @Tomalak because I am saving data from a dynamic page to this db, and I want to check if the data on the page got updated, so I get the last row I inserted to the db, and check if it's not equal -> then there is new data. – TheDragoner Mar 23 '18 at 13:38
  • TinyDB doesn't have aggregation so you can't just query for a max, but you can create a sufficient substitute. Store each instance of page data with a time and store when you last fetched. When checking for new data, get when you last fetched, get the page data corresponding to that time, then do your compare. – Backgammon Mar 24 '18 at 01:45

1 Answers1

1

You can use doc_id for that case since the last row will the largest index

last_row = db.get(doc_id=len(db))
enpith
  • 1,540
  • 2
  • 10
  • 11