C
C
che_aa2021-04-10 15:55:42
Python
che_aa, 2021-04-10 15:55:42

How to get latest sqlite records?

How to get records in reverse order sqlite? An important clarification, there is no id or similar sorting options. https://zametkinapolyah.ru/zametki-o-mysql/chast-1... this guide is not suitable in my case.
There is a code:

import sqlite3

db = sqlite3.connect('database.db', check_same_thread = False)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS test (
  id BIGINT,
  call TEXT
)''')
t = 10
for i in range(10):
  cursor.execute(f"INSERT INTO test VALUES (?, ?)", (i, t-1))
  db.commit()
check = cursor.execute(f'SELECT * FROM test')
search = check.fetchall()
print(search)

Result of work:
[(0, 10), (1, 9), (2, 8), (3, 7), (4, 6), (5, 5), (6, 4), (7, 3), (8, 2), (9, 1)]

How can I get the data in reverse order?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alan Gibizov, 2021-04-10
@che_aa

Before print add
search.reverse()

R
rPman, 2021-04-10
@rPman

sql databases, in the standard, do not guarantee the order of the results in a query if order by is not specified (an exception, group by, you need to read the documentation for each database used and the use of specific indexes).
The fact that if it works as you expect in 1000 cases, in 1001 you will get an unexpected result (a lot of situations, unsuccessful combinations of adding and deleting records, a strange combination of records in the index, etc.)
in sqlite in each table, if specially don't remove it on creation, there is a rowid column which is the id of the entry, and it's a positive number, and it increments when added (reset by vacuum), maybe you have enough, sort by it.

Y
Yupiter7575, 2021-04-10
@yupiter7575

Add as normal people Id. And exactly...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question