Why is this only adding in the first entry import sqlite3 import logging from…
- User
Why is this only adding in the first entry
import sqlite3 import logging from scrape_weather import WeatherScraper
class DBOperations: """Class for performing operations on a SQLite database"""
def __init__(self, dbname): """ Constructor for DBOperations class. Parameters: - dbname: str, the name of the SQLite database file to use """ self.dbname = dbname self.logger = logging.getLogger(__name__) def initialize_db(self): """ Initialize the SQLite database by creating the weather_data table. This method should be called every time the program runs. """ with self.get_cursor() as cursor: try: cursor.execute(''' CREATE TABLE IF NOT EXISTS weather_data ( id INTEGER PRIMARY KEY AUTOINCREMENT, sample_date TEXT UNIQUE, location TEXT, min_temp REAL, max_temp REAL, avg_temp REAL ) ''') self.logger.info("Initialized database successfully.") except sqlite3.Error as e: self.logger.error(f"An error occurred while creating the table: {e}") def save_data(self, data): """ Save weather data to the SQLite database. If the data already exists in the database, it will not be duplicated. Parameters: - data: dict, the weather data to save to the database. Must have keys for sample_date, location, min_temp, max_temp, and avg_temp. """ with self.get_cursor() as cursor: try: cursor.execute(''' INSERT OR IGNORE INTO weather_data (sample_date, location, min_temp, max_temp, avg_temp) VALUES (?, ?, ?, ?, ?) ''', (data['sample_date'], data['location'], data['min_temp'], data['max_temp'], data['avg_temp'])) self.logger.info("Data saved successfully.") except sqlite3.Error as e: self.logger.error(f"An error occurred while saving data to the database: {e}") def fetch_data(self, location): """ Fetch weather data from the SQLite database for a specified location. Parameters: - location: str, the location to fetch weather data for Returns: - A list of tuples containing the weather data for the specified location, where each tuple has the format (sample_date, min_temp, max_temp, avg_temp). Returns an empty list if no data is found for the specified location. """ with self.get_cursor() as cursor: try: cursor.execute(''' SELECT sample_date, min_temp, max_temp, avg_temp FROM weather_data WHERE location = ? ''', (location,)) data = cursor.fetchall() self.logger.info("Data fetched successfully.") return data except sqlite3.Error as e: self.logger.error(f"An error occurred while fetching data from the database: {e}") return [] def purge_data(self): """ Purge all weather data from the SQLite database. """ with self.get_cursor() as cursor: try: cursor.execute('DELETE FROM weather_data') self.logger.info("Data purged successfully.") except sqlite3.Error as e: self.logger.error(f"An error occurred while purging data from the database: {e}") def get_cursor(self): """ Get a cursor to use for database operations. Returns: - A cursor object for the SQLite database. """ return DBCM(self.dbname) - Assistant
It seems that the problem may lie in the
WeatherScraperclass, as theDBOperationsclass, doesn't seem to have any issues with how it's adding data to the database.Upon further inspection, I believe I found a possible issue in your
WeatherScraperclass. In thehandle_datamethod, theself.daily_tempsdictionary is being reused, which might be causing issues when adding data to theself.weatherdictionary.I recommend modifying the line where you reassign
self.daily_tempsto an empty dictionary, as follows:Replace this code:
self.daily_temps = {}With this code:
self.daily_temps = {"Max": None, "Min": None, "Mean": None}This change ensures that the
self.daily_tempsdictionary always has the keys "Max", "Min", and "Mean" with None as their initial value.If this doesn't solve the issue, please provide some more details about the problem you're facing, such as any errors or unexpected behavior you observe while running the code.