So far we’ve managed to:
- Query a database for weather records by location.
- Reformat that data into a JavaScript object.
- Manipulate that JavaScript object to find new, meaningful information.
That’s pretty significant! Now it’s time to take that useful information and add it to a table of our own. Since creating a table is another operation that does not return a row, we can use the same db.run()
we used to INSERT
rows. Let’s see what happens when we CREATE
a TABLE
and INSERT
our data into it.
Notice there’s a statement declaring “DROP TABLE IF EXISTS” — this is because we want to make sure when we create our new table that we won’t run into an error telling us the table already exists (from previous times running our code).
Instructions
Use db.run()
to CREATE
a new table Average
with id
, year
, and temperature
columns.
id: INTEGER PRIMARY KEY year: INTEGER NOT NULL temperature: REAL NOT NULL
After creating your table with db.run()
, iterate through your averageTemperatureByYear
array and INSERT
each into your table using a db.run()
.
It looks like an error was thrown. Add a callback after your INSERT
query in db.run()
and log the error to the console if it exists.
We’ve triggered a lot of errors! It looks like some INSERT
s are happening before the table has been created. Click ‘Next’ to find out why this is happening and how to fix it.