You want to store sensor readings with a timestamp in SQLite on Raspberry Pi. Which table schema and insertion code correctly store sensor name, value, and current time using Python's sqlite3?
hard🚀 Application Q15 of 15
Raspberry Pi - Data Logging and Databases
You want to store sensor readings with a timestamp in SQLite on Raspberry Pi. Which table schema and insertion code correctly store sensor name, value, and current time using Python's sqlite3?
ACREATE TABLE readings (name TEXT, value REAL, time TEXT); cursor.execute("INSERT INTO readings VALUES ('temp', 22.1, 'CURRENT_TIMESTAMP')")
BCREATE TABLE readings (name TEXT, value REAL, time INTEGER); cursor.execute("INSERT INTO readings VALUES ('temp', 22.1, CURRENT_TIMESTAMP)")
CCREATE TABLE readings (name TEXT, value REAL, time INTEGER); cursor.execute("INSERT INTO readings VALUES ('temp', 22.1, datetime('now'))")
DCREATE TABLE readings (name TEXT, value REAL, time TEXT); cursor.execute("INSERT INTO readings VALUES ('temp', 22.1, datetime('now', 'localtime'))")
Step-by-Step Solution
Solution:
Step 1: Choose correct table schema for timestamp
Use TEXT type for time column to store datetime strings, which SQLite supports well.
Step 2: Insert current local time correctly
Use SQLite function datetime('now', 'localtime') to get current local timestamp as text.
Step 3: Match insertion code with schema
CREATE TABLE readings (name TEXT, value REAL, time TEXT); cursor.execute("INSERT INTO readings VALUES ('temp', 22.1, datetime('now', 'localtime'))") uses TEXT time column and inserts with datetime('now', 'localtime'), which is correct.
Final Answer:
CREATE TABLE readings (name TEXT, value REAL, time TEXT); cursor.execute("INSERT INTO readings VALUES ('temp', 22.1, datetime('now', 'localtime'))") -> Option D
Quick Check:
Local timestamp stored as TEXT = A [OK]
Quick Trick:Use datetime('now', 'localtime') for current local time in SQLite [OK]
Common Mistakes:
MISTAKES
Using CURRENT_TIMESTAMP without quotes in Python
Storing time as INTEGER without conversion
Ignoring localtime adjustment for timestamps
Master "Data Logging and Databases" in Raspberry Pi
9 interactive learning modes - each teaches the same concept differently