Bird
0
0

Why does this Python code raise an error when executed on Raspberry Pi?

medium📝 Debug Q7 of 15
Raspberry Pi - Data Logging and Databases
Why does this Python code raise an error when executed on Raspberry Pi?
import sqlite3
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM sensor_data')
print(cursor.fetchall())
conn.close()

Assuming the table sensor_data has not been created yet.
AThe table 'sensor_data' does not exist in the database
BThe fetchall() method is called before execute()
CThe database file 'data.db' cannot be opened
DThe connection is closed before executing the query
Step-by-Step Solution
Solution:
  1. Step 1: Understand the error cause

    Executing SELECT on a non-existent table causes an OperationalError in SQLite.
  2. Step 2: Verify code logic

    The code tries to query 'sensor_data' which is not created, so SQLite raises an error.
  3. Final Answer:

    Table 'sensor_data' does not exist -> Option A
  4. Quick Check:

    Check table creation before querying [OK]
Quick Trick: Create tables before querying them [OK]
Common Mistakes:
MISTAKES
  • Querying tables before creating them
  • Assuming database file exists with required tables
  • Closing connection prematurely

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes