Sensor log storage and queries

English translation

Use the device log structure to check whether storing the same key again creates a new record or changes the existing record.

Before practice

01 How should you store a measurement that arrives again?

Description

Use the device and measurement time as the key, and define a duplicate handling rule such as updating or ignoring.

02 Is having no data the same as an invalid request?

Description

Distinguish intervals with no data, which return empty results, from uninterpretable intervals, which are input errors.

Execution conditions

Uses Python's sqlite3; no DB server or account is required.

Practice

Preview practice notebookDownload practice notebook

1. Store duplicate measurements only once

Starting conditions

The measurements for device m1 at t∈{0,1,1,2}t\in\{0,1,1,2\} s are 20,21,22,23. Update entries with the same device and time to the latest value.

Procedure

  1. Check SQLite's composite primary key and store the four provided measurements.
  2. Check the number of stored rows and the value of t=1t=1, and explain whether the duplicate handling rule is correct.

Results to retain

Items to record and points to check
Input count
Stored row count
Duplicate key
Final value

Points to check

  • Did you distinguish measurement time from reception time?
  • Did you use parameters instead of appending values directly to SQL strings?

2. Query by time interval and device

Starting conditions

Add t∈{0,1,2}t\in\{0,1,2\} with values 30,31,32 for device m2. Query only m1 for 1≤t≤2.

Procedure

  1. Write a query function that takes a device, start time, and end time as arguments.
  2. Test the specified interval and an interval with no values once each.
  3. Plot query results as time versus measurement.

Results to retain

Items to record and points to check
Device
Query interval
Number of rows
Value range

Points to check

  • Did you specify the sort order in SQL?
  • Did you avoid replacing empty results with errors or a value of 0?

3. Connect sensors to an API

Starting conditions

Assume the HTTP API in the next unit calls the query function. The request is device=m1,start=0,end=2.

Procedure

  1. Define the field structure for passing query results as JSON.
  2. Define response rules that distinguish a nonexistent device from start>end.
  3. Identify the data and query rules to retain when switching to an external storage system such as DynamoDB.

Results to retain

Items to record and points to check
Request
Validation results
Response fields
Expected row count

Points to check

  • Did you verify storage and query principles without an external service account?
  • Did you preserve the meanings of device ID and measurement time in the API?

Official examples and data