From d732f3ad2d73062c5d7ca2be1677173935bf15bd Mon Sep 17 00:00:00 2001 From: PaulVua Date: Thu, 30 Jan 2025 17:24:47 +0100 Subject: [PATCH] update --- .gitignore | 3 ++- sqlite/create_db.py | 27 +++++++++++++++++++++++++++ sqlite/read.py | 23 +++++++++++++++++++++++ sqlite/write.py | 23 +++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 sqlite/create_db.py create mode 100644 sqlite/read.py create mode 100644 sqlite/write.py diff --git a/.gitignore b/.gitignore index d919be2..cdbec4c 100755 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ envea/data/*.txt envea/data/*.json NPM/data/*.txt NPM/data/*.json -*.lock \ No newline at end of file +*.lock +sqlite/*.db \ No newline at end of file diff --git a/sqlite/create_db.py b/sqlite/create_db.py new file mode 100644 index 0000000..3df266b --- /dev/null +++ b/sqlite/create_db.py @@ -0,0 +1,27 @@ +''' +Script to create a sqlite database +/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/create_db.py + +''' + +import sqlite3 + +# Connect to (or create) the database +conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") +cursor = conn.cursor() + +# Create a table for storing sensor data +cursor.execute(""" +CREATE TABLE IF NOT EXISTS data ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, + sensor TEXT, + value REAL +) +""") + +# Commit and close the connection +conn.commit() +conn.close() + +print("Database and table created successfully!") diff --git a/sqlite/read.py b/sqlite/read.py new file mode 100644 index 0000000..b58f27a --- /dev/null +++ b/sqlite/read.py @@ -0,0 +1,23 @@ +''' +Script to read data from a sqlite database +/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/read.py + +''' + +import sqlite3 + +# Connect to the SQLite database +conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") +cursor = conn.cursor() + +# Retrieve the last 10 sensor readings +cursor.execute("SELECT * FROM data ORDER BY timestamp DESC LIMIT 10") + +rows = cursor.fetchall() + +# Display the results +for row in rows: + print(row) + +# Close the database connection +conn.close() diff --git a/sqlite/write.py b/sqlite/write.py new file mode 100644 index 0000000..cd5da62 --- /dev/null +++ b/sqlite/write.py @@ -0,0 +1,23 @@ +''' +Script to write data to a sqlite database +/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/write.py + +''' + +import sqlite3 + +# Connect to the SQLite database +conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") +cursor = conn.cursor() + +# Insert a sample temperature reading +sensor_name = "temperature" +sensor_value = 25.3 + +cursor.execute("INSERT INTO data (sensor, value) VALUES (?, ?)", (sensor_name, sensor_value)) + +# Commit and close the connection +conn.commit() +conn.close() + +print("Sensor data saved successfully!")