May 2018
Beginner to intermediate
452 pages
11h 26m
English
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, ...