Bird
0
0

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:
  1. Step 1: Choose correct table schema for timestamp

    Use TEXT type for time column to store datetime strings, which SQLite supports well.
  2. Step 2: Insert current local time correctly

    Use SQLite function datetime('now', 'localtime') to get current local timestamp as text.
  3. 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.
  4. 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
  5. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes