Bird
0
0

Identify the error in this Python code snippet for inserting sensor data into SQLite on Raspberry Pi:

medium📝 Debug Q6 of 15
Raspberry Pi - Data Logging and Databases
Identify the error in this Python code snippet for inserting sensor data into SQLite on Raspberry Pi:
import sqlite3
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO readings (value) VALUES 30')
conn.commit()
conn.close()
AThe VALUES keyword is missing parentheses around 30
BThe table name 'readings' is misspelled
CThe connection to the database is not established
DThe commit() method is called before execute()
Step-by-Step Solution
Solution:
  1. Step 1: Check SQL syntax

    The INSERT statement requires VALUES followed by parentheses enclosing the value(s).
  2. Step 2: Identify the error

    The code uses VALUES 30 without parentheses, which is invalid SQL syntax.
  3. Final Answer:

    VALUES 30 should be VALUES (30) -> Option A
  4. Quick Check:

    Correct SQL syntax for INSERT [OK]
Quick Trick: Always enclose VALUES in parentheses [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses around values in INSERT
  • Misspelling table names
  • Calling commit before execute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes