Learn
Learn Node SQLite
Creating A New Table

So far we’ve managed to:

  1. Query a database for weather records by location.
  2. Reformat that data into a JavaScript object.
  3. 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

1.

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
2.

After creating your table with db.run(), iterate through your averageTemperatureByYear array and INSERT each into your table using a db.run().

3.

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.

4.

We’ve triggered a lot of errors! It looks like some INSERTs are happening before the table has been created. Click ‘Next’ to find out why this is happening and how to fix it.

Folder Icon

Sign up to start coding

Already have an account?