Implementing the SQLModel.add_weather_data() method

Over in models.py, let's add a new method to the SQLModel class called add_weather_data(), which takes a data dict as its only argument.

Let's start this method by writing an INSERT query as follows:

    def add_weather_data(self, data):
        query = (
            'INSERT INTO local_weather VALUES '
            '(%(observation_time_rfc822)s, %(temp_c)s, '
            '%(relative_humidity)s, %(pressure_mb)s, '
            '%(weather)s)'
        )

This is a straightforward parameterized INSERT query using variable names that match the dict keys that the get_local_weather() function extracts from the XML data. We should only need to pass this query and the data dict into our query() method.

There is one problem, however; if we get a duplicate timestamp, ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.