From 857d590b8f50562a0a5c5b64a17d537819d76ffd Mon Sep 17 00:00:00 2001 From: PaulVua Date: Wed, 7 Jan 2026 14:38:22 +0100 Subject: [PATCH] Order database table display by insertion order (ROWID) instead of timestamp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changed the data retrieval query to use ROWID DESC instead of timestamp DESC, ensuring that the most recently inserted data appears first regardless of timestamp field values. This fixes the issue where entries with "not connected" or invalid timestamps would appear in wrong order. Benefits: - Most recent database entries always shown at top - Works correctly even when timestamp is null, "not connected", or incorrect - Based on actual insertion order rather than timestamp field 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- sqlite/read.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/sqlite/read.py b/sqlite/read.py index c5a47ea..d12abc3 100755 --- a/sqlite/read.py +++ b/sqlite/read.py @@ -30,19 +30,17 @@ limit_num=parameter[1] conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") cursor = conn.cursor() -# Retrieve the last 10 sensor readings -#cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 10") -#cursor.execute("SELECT * FROM data_BME280 ORDER BY timestamp DESC LIMIT 10") -#cursor.execute("SELECT * FROM timestamp_table") +# Retrieve the last sensor readings based on insertion order (ROWID) +# This ensures we get the most recently inserted data, regardless of timestamp value if table_name == "timestamp_table": cursor.execute("SELECT * FROM timestamp_table") else: - query = f"SELECT * FROM {table_name} ORDER BY timestamp DESC LIMIT ?" + # Order by ROWID DESC to get most recently inserted rows first + query = f"SELECT * FROM {table_name} ORDER BY ROWID DESC LIMIT ?" cursor.execute(query, (limit_num,)) - rows = cursor.fetchall() -rows.reverse() # Reverse the order in Python (to get ascending order) +# Keep DESC order - most recently inserted data first # Display the results