Skip to content
Snippets Groups Projects
Commit 587a5235 authored by Wortman, Elliot (UG - Comp Sci & Elec Eng)'s avatar Wortman, Elliot (UG - Comp Sci & Elec Eng)
Browse files

Sqlite database read & write functions

parent d89e71e6
No related branches found
No related tags found
No related merge requests found
......@@ -30,3 +30,4 @@ coverage
__pycache__/
env/
archive.zip
measurements.db
from flask import Flask, jsonify, render_template, send_from_directory
from flask_cors import CORS
from db_sqlite import read_latest_entry_as_json
app = Flask(__name__, static_folder="./dist/assets", template_folder="./dist")
CORS(app)
......@@ -12,8 +14,7 @@ def main():
@app.route("/values")
def values():
values = [{"N": 10}, {"P": 11}, {"K": 12}, {"T": 5}, {"H": 6}, {"M": 7}]
return jsonify(values)
return read_latest_entry_as_json()
@app.route("/past-values")
def pastValues():
......
import sqlite3
import json
DB_FILE = "measurements.db"
def insert_measurement(input_values):
""" Insert values into the database and fetch the latest entry """
try:
# Create a database connection
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Create table if it doesn't exist
cursor.execute('''CREATE TABLE IF NOT EXISTS measurements (
nitrogen INTEGER,
phosphorus INTEGER,
potassium INTEGER,
temperature INTEGER,
humidity INTEGER,
moisture INTEGER
);''')
# Insert new values
cursor.execute('''INSERT INTO measurements
(nitrogen, phosphorus, potassium, temperature, humidity, moisture)
VALUES (?, ?, ?, ?, ?, ?)''', input_values)
conn.commit()
# Fetch the latest entry
cursor.execute(
"SELECT * FROM measurements ORDER BY rowid DESC LIMIT 1;")
row = cursor.fetchone()
# Close the connection
conn.close()
# Process and return the result
if row:
column_names = ['nitrogen', 'phosphorus',
'potassium', 'temperature', 'humidity', 'moisture']
data = dict(zip(column_names, row))
return json.dumps(data, indent=4)
else:
return json.dumps({})
except sqlite3.Error as e:
print(e)
return None
def read_latest_entry_as_json():
""" Fetch the latest entry from the database and return it as a list of dicts
such as [{"N": 10}, {"P": 11}, {"K": 12}, {"T": 5}, {"H": 6}, {"M": 7}] """
try:
# Create a database connection
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Fetch the latest entry
cursor.execute(
"SELECT * FROM measurements ORDER BY rowid DESC LIMIT 1;")
row = cursor.fetchone()
# Close the connection
conn.close()
# Process and return the result
if row:
column_names = ['N', 'P', 'K', 'T', 'H', 'M']
data = [{column_names[i]: row[i]} for i in range(len(row))]
return json.dumps(data)
else:
return json.dumps([])
except sqlite3.Error as e:
print(e)
return None
......@@ -43,6 +43,6 @@ if [ ! -f "app.py" ]; then
fi
# Create a zip file
zip -r demo.zip requirements.txt app.py dist
zip -r demo.zip requirements.txt app.py db_sqlite.py dist
echo "Archive 'demo.zip' created successfully."
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment