0
0
Raspberry Piprogramming~10 mins

SQLite database for sensor data in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SQLite database for sensor data
Start Program
Connect to SQLite DB
Create Table if not exists
Read Sensor Data
Insert Data into Table
Commit Transaction
Close Connection
End Program
The program connects to a SQLite database, creates a table if needed, reads sensor data, inserts it into the table, commits changes, and closes the connection.
Execution Sample
Raspberry Pi
import sqlite3
conn = sqlite3.connect('sensor.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS readings (id INTEGER PRIMARY KEY, temperature REAL, humidity REAL)''')
cursor.execute('INSERT INTO readings (temperature, humidity) VALUES (?, ?)', (23.5, 60.2))
conn.commit()
conn.close()
This code creates a database and table, inserts one sensor reading, saves it, and closes the database.
Execution Table
StepActionSQL Command / OperationResult / State
1Connect to DBsqlite3.connect('sensor.db')Database file 'sensor.db' opened or created
2Create cursorconn.cursor()Cursor object created for SQL commands
3Create table if not existsCREATE TABLE IF NOT EXISTS readings (id INTEGER PRIMARY KEY, temperature REAL, humidity REAL)Table 'readings' ready in DB
4Insert sensor dataINSERT INTO readings (temperature, humidity) VALUES (23.5, 60.2)One row inserted with temperature=23.5, humidity=60.2
5Commit transactionconn.commit()Changes saved to database
6Close connectionconn.close()Database connection closed
💡 Program ends after closing the database connection
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
connNoneConnection objectConnection objectConnection objectConnection objectConnection objectClosed
cursorNoneNoneCursor objectCursor objectCursor objectCursor objectNone
Key Moments - 3 Insights
Why do we need to commit after inserting data?
Committing saves changes permanently in the database. Without commit, inserted data won't be stored, as shown in step 5 of the execution_table.
What happens if the table already exists when we run CREATE TABLE?
Using 'IF NOT EXISTS' prevents errors by only creating the table if it doesn't exist, as seen in step 3.
Why close the database connection at the end?
Closing frees resources and ensures data integrity, as shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'conn' after step 6?
AConnection open
BConnection closed
CCursor object
DNone
💡 Hint
Check variable_tracker row for 'conn' after step 6
At which step is the sensor data actually saved permanently in the database?
AStep 4 - Insert sensor data
BStep 3 - Create table
CStep 5 - Commit transaction
DStep 6 - Close connection
💡 Hint
Look at execution_table step 5 description about commit
If we remove 'IF NOT EXISTS' from the CREATE TABLE command, what could happen?
AProgram will fail if table exists
BData will be lost
CTable will be created anyway without issues
DDatabase connection will close early
💡 Hint
Refer to key_moments about table creation errors
Concept Snapshot
SQLite database for sensor data:
- Connect with sqlite3.connect('file.db')
- Create table with CREATE TABLE IF NOT EXISTS
- Insert data with INSERT INTO
- Commit changes with conn.commit()
- Close connection with conn.close()
- Always commit to save data permanently
Full Transcript
This example shows how to use SQLite on a Raspberry Pi to store sensor data. First, the program connects to a database file or creates it if missing. Then it creates a table for readings if it doesn't exist yet. Next, it inserts one sensor reading with temperature and humidity values. After inserting, it commits the transaction to save the data permanently. Finally, it closes the database connection to free resources. Key points include always committing after inserts and using 'IF NOT EXISTS' to avoid errors when creating tables. Closing the connection is important for data integrity.