Order database table display by insertion order (ROWID) instead of timestamp
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user