Browse Source

Can get average of sensor data from a discrict from last week

tsetsova 2 years ago
parent
commit
9d90085bb2
2 changed files with 55 additions and 19 deletions
  1. 36 8
      sqlite/README.md
  2. 19 11
      sqlite/script.py

+ 36 - 8
sqlite/README.md

@@ -1,6 +1,12 @@
1 1
 # SQLite Spike
2 2
 
3
-## Simple query examples with SQL:
3
+## Setting up
4
+
5
+- Open your terminal and start SQLite with the name of the database:
6
+
7
+```
8
+sqlite3 db-name.db
9
+```
4 10
 
5 11
 - Create a table:
6 12
 
@@ -11,14 +17,16 @@ CREATE TABLE data (deviceId integer, timestamp datetime, sensorReading integer,
11 17
 - Add data to table:
12 18
 
13 19
 ```
14
-INSERT INTO data VALUES (12, datetime('-200 days'), 13, 'E9'),
15
-                        (12, datetime('-5 days'), 12, 'E9'),
16
-                        (8, datetime('-6 days'), 14, 'E9'),
17
-                        (34, datetime('-150 days'), 80, 'E2'),
18
-                        (45, datetime('-5 days'), 76, 'E2'),
19
-                        (34, datetime('-6 days'), 70, 'E2');
20
+INSERT INTO data VALUES (12, datetime('now', '-200 days'), 13, 'E9'),
21
+                        (12, datetime('now', '-5 days'), 12, 'E9'),
22
+                        (8, datetime('now', '-6 days'), 14, 'E9'),
23
+                        (34, datetime('now', '-150 days'), 80, 'E2'),
24
+                        (45, datetime('now', '-5 days'), 76, 'E2'),
25
+                        (34, datetime('now', '-6 days'), 70, 'E2');
20 26
 ```
21 27
 
28
+## Simple query examples with SQL
29
+
22 30
 - Query: give all reading for district E2
23 31
 
24 32
 ```
@@ -28,5 +36,25 @@ SELECT * FROM data WHERE district IS 'E9';
28 36
 - Query: All data since last week in E2
29 37
 
30 38
 ```
31
-SELECT * FROM data WHERE timestamp >= datetime('-7 days') AND district IS 'E2';
39
+SELECT * FROM data WHERE timestamp >= datetime('now', '-7 days') AND district IS 'E2';
40
+```
41
+
42
+- Query: Average data since last week in district 'E2'
43
+
44
+```
45
+SELECT avg(sensorReading) FROM data WHERE timestamp >= datetime('now', '-7 days') AND district IS 'E2';
46
+```
47
+
48
+## Clean up
49
+
50
+- Dropping the table:
51
+
52
+```
53
+DROP TABLE data;
32 54
 ```
55
+
56
+## Resources
57
+
58
+- [Query for last day/ last week/ last month](http://stackoverflow.com/questions/10504218/query-last-day-last-week-last-month-sqlite)
59
+- [SQLite date/time functions docs](https://www.sqlite.org/lang_datefunc.html)
60
+- [Simple Python / SQLite tutorial](https://www.blog.pythonlibrary.org/2012/07/18/python-a-simple-step-by-step-sqlite-tutorial/)

+ 19 - 11
sqlite/script.py

@@ -8,9 +8,9 @@ db = sqlite3.connect('data-table.db')
8 8
 cursor = db.cursor()
9 9
 
10 10
 cursor.execute(""" CREATE TABLE data
11
-                    (deviceId integer, timestamp integer, value integer, district text)
11
+                    (deviceId integer, timestamp integer, sensorReading integer, district text)
12 12
             """)
13
-print("Table created")
13
+print("Table created\n")
14 14
 
15 15
 cursor.execute(""" INSERT INTO data VALUES (12, datetime('-200 days'), 13, 'E9'),
16 16
                         (12, datetime('now', '-150 days'), 12, 'E9'),
@@ -18,17 +18,25 @@ cursor.execute(""" INSERT INTO data VALUES (12, datetime('-200 days'), 13, 'E9')
18 18
                         (34, datetime('now', '-20 days'), 80, 'E2'),
19 19
                         (45, datetime('now', '-5 days'), 76, 'E2'),
20 20
                         (34, datetime('now', '-6 days'), 70, 'E2'); """)
21
-print("Values inserted")
21
+print("Values inserted\n")
22 22
 
23 23
 db.commit()
24 24
 
25 25
 print("Query: give all reading for district E2")
26
-cursor.execute(""" SELECT * FROM data WHERE district IS 'E2' """)
27
-
28
-print("Query: All data since last week in E2")
29
-cursor.execute(""" SELECT * FROM data WHERE timestamp >= datetime('now', '-7 days') AND district IS 'E2' """)
30
-
26
+results = cursor.execute(""" SELECT * FROM data WHERE district IS 'E2' """).fetchall()
27
+for result in results:
28
+    print(result)
29
+print("\n")
30
+
31
+print("Query: All data since last week for district E2")
32
+results = cursor.execute(""" SELECT * FROM data WHERE timestamp >= datetime('now', '-7 days') AND district IS 'E2' """).fetchall()
33
+for result in results:
34
+    print(result)
35
+print("\n")
36
+
37
+print("Query: Average data since last week for district E2")
38
+result = cursor.execute(""" SELECT avg(sensorReading) FROM data WHERE timestamp >= datetime('now', '-7 days') AND district IS 'E2' """).fetchone()[0]
39
+print(result)
40
+print("\n")
31 41
 db.close()
32
-
33
-# cursor.execute(""" DROP TABLE data """)
34
-# print("Table dropped")
42
+print("You can continue querying on your own or drop the table 'data'")