You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmftools/WeatherApp/modules/database.py

40 lines
1.2 KiB

import mysql.connector
class Database:
def __init__(self, config, logger):
self.config = config
self.logger = logger
def connect(self):
"""Maakt een databaseverbinding."""
try:
conn = mysql.connector.connect(**self.config)
return conn
except mysql.connector.Error as err:
self.logger.error(f"Database connectiefout: {err}")
return None
def get_weather_data(self):
"""Haalt de gemiddelde weergegevens per dag op."""
conn = self.connect()
if not conn:
return []
cursor = conn.cursor(dictionary=True)
query = """
SELECT DATE(utc) as date,
AVG(temperature) as avg_temp,
AVG(solar_performance) as avg_solar,
AVG(wind_speed) as avg_wind_speed,
GROUP_CONCAT(DISTINCT weather_description) as descriptions
FROM weather_forecast
WHERE DATE(utc) >= DATE(NOW())
GROUP BY DATE(utc)
ORDER BY date ASC
"""
cursor.execute(query)
data = cursor.fetchall()
conn.close()
return data