我试图从我的tinyDB获取最后一条记录,所以我想做一个看起来像这样的查询:
"SELECT * FROM table ORDER BY id DESC LIMIT 1"
哪个应该给我最后一排。虽然我无法弄清楚如何使用TinyDB。
答案 0 :(得分:0)
如果您想按时间降序排序db,例如:
od = sorted(db.all(), key=lambda k: k['time'])
print(od[-1])
答案 1 :(得分:0)
答案 2 :(得分:0)
使用Query
并进行更新:
with TinyDB('db.json') as db:
my_table = db.table('a_table_name')
my_query= Query()
first_of_table_for_this_query = my_table.search(my_query.some_field == some_value)[1]
last_of_table_for_this_query = my_table.search(my_query.some_field == some_value)[-1]
# exemple of use for updating first inserted and last inserted only
my_table.update({'some_field': some_value+42}, doc_ids=[
first_of_table_for_this_query,
last_of_table_for_this_query
])