Files
nebuleair_pro_4g/sqlite/create_db.py
2025-10-07 15:03:11 +02:00

144 lines
2.9 KiB
Python
Executable File

r'''
____ ___ _ _ _
/ ___| / _ \| | (_) |_ ___
\___ \| | | | | | | __/ _ \
___) | |_| | |___| | || __/
|____/ \__\_\_____|_|\__\___|
Script to create a sqlite database
/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/create_db.py
in case of readonly error:
sudo chmod 777 /var/www/nebuleair_pro_4g/sqlite/sensors.db
'''
import sqlite3
# Connect to (or create if not existent) the database
conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db")
cursor = conn.cursor()
#create a config table
cursor.execute("""
CREATE TABLE IF NOT EXISTS config_table (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
type TEXT NOT NULL
)
""")
#creates a config table for envea sondes
cursor.execute("""
CREATE TABLE IF NOT EXISTS envea_sondes_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
connected INTEGER NOT NULL,
port TEXT NOT NULL,
name TEXT NOT NULL,
coefficient REAL NOT NULL
)
""")
# Create a table timer
cursor.execute("""
CREATE TABLE IF NOT EXISTS timestamp_table (
id INTEGER PRIMARY KEY CHECK (id = 1), -- Enforce single row by using fixed ID
last_updated DATETIME NOT NULL
)
""")
cursor.execute("""
INSERT OR REPLACE INTO timestamp_table (id, last_updated)
VALUES (1, CURRENT_TIMESTAMP);
""")
#create a modem status table
cursor.execute("""
CREATE TABLE IF NOT EXISTS modem_status (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
status TEXT
)
""")
# Create a table NPM
cursor.execute("""
CREATE TABLE IF NOT EXISTS data_NPM (
timestamp TEXT,
PM1 REAL,
PM25 REAL,
PM10 REAL,
temp_npm REAL,
hum_npm REAL
)
""")
# Create a table BME280
cursor.execute("""
CREATE TABLE IF NOT EXISTS data_BME280 (
timestamp TEXT,
temperature REAL,
humidity REAL,
pressure REAL
)
""")
# Create a table cairsens
cursor.execute("""
CREATE TABLE IF NOT EXISTS data_envea (
timestamp TEXT,
no2 REAL,
h2s REAL,
nh3 REAL,
co REAL,
o3 REAL,
so2 REAL
)
""")
# Create a table NPM_5ch
cursor.execute("""
CREATE TABLE IF NOT EXISTS data_NPM_5channels (
timestamp TEXT,
PM_ch1 INTEGER,
PM_ch2 INTEGER,
PM_ch3 INTEGER,
PM_ch4 INTEGER,
PM_ch5 INTEGER
)
""")
# Create a table WIND
cursor.execute("""
CREATE TABLE IF NOT EXISTS data_WIND (
timestamp TEXT,
wind_speed REAL,
wind_direction REAL
)
""")
# Create a table MPPT
cursor.execute("""
CREATE TABLE IF NOT EXISTS data_MPPT (
timestamp TEXT,
battery_voltage REAL,
battery_current REAL,
solar_voltage REAL,
solar_power REAL,
charger_status INTEGER
)
""")
# Create a table noise capture (NSRT mk4)
cursor.execute("""
CREATE TABLE IF NOT EXISTS data_NOISE (
timestamp TEXT,
current_LEQ REAL,
DB_A_value REAL
)
""")
# Commit and close the connection
conn.commit()
conn.close()
print("Database and table created successfully!")