FAQ: Learn Node SQLite - Creating A New Table

This community-built FAQ covers the “Creating A New Table” exercise from the lesson “Learn Node SQLite”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Node-SQLite

FAQs on the exercise Creating A New Table

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m confused on the first step. When I enter my db.run where the code editor says “your code here”, I get this error:

/home/ccuser/workspace/node-sqlite-learn-node-sqlite-creating-a-new-table/app.js:25
  db.run('CREATE TABLE Average 
         ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid or unexpected token

I can’t figure out why. Here is my code:

const { printQueryResults, calculateAverages, addClimateRowToObject } = require('./utils');
const sqlite = require('sqlite3');

const db = new sqlite.Database('./db.sqlite');

const temperaturesByYear = {};

db.run('DROP TABLE IF EXISTS Average', error => {
  if (error) {
    throw error;
  }
  db.each('SELECT * FROM TemperatureData',
    (error, row) => {
      if (error) {
        throw error;
      }
      addClimateRowToObject(row, temperaturesByYear);
    }, 
    error => {
      if (error) {
        throw error;
      }
      const averageTemperatureByYear = calculateAverages(temperaturesByYear);
      // your code here:
  db.run('CREATE TABLE Average 
  (id INTEGER PRIMARY KEY, 
  year INTEGER NOT NULL, 
  temperature REAL NOT NULL)', logNodeError); 
  )
    }
  );
});

1 Like

callmej9 in this exercise the solution to step one would create an error in real life. However that is not why Codecademy will not let you move past the first step and keeps creating not only an “wrong answer” response but that error code you keep getting. These algo’s are very specific and in some cases a little too specific. You have one to many right hand parentheses " ) " and you should remove the logNodeError for this step. You are right it should be placed there in real life. But for the step take it out. The algo wants to make sure you get this “part” of the concept before letting you move on. Below is the answer for THIS STEP

const { printQueryResults, calculateAverages, addClimateRowToObject } = require(’./utils’);
const sqlite = require(‘sqlite3’);

const db = new sqlite.Database(’./db.sqlite’);

const temperaturesByYear = {};

db.run(‘DROP TABLE IF EXISTS Average’, error => {
if (error) {
throw error;
}
db.each(‘SELECT * FROM TemperatureData’,
(error, row) => {
if (error) {
throw error;
}
addClimateRowToObject(row, temperaturesByYear);
},
error => {
if (error) {
throw error;
}
const averageTemperatureByYear = calculateAverages(temperaturesByYear);
// your code here:
db.run(‘CREATE TABLE Average (id INTEGER PRIMARY KEY, year INTEGER NOT NULL, temperature REAL NOT NULL)’);
}
);
});

2 Likes

p.s.
also… this is a fantastic question because I had a similar problem The Codecademy software tests check our answers and in many cases the answer to the specific “step” is not the same as the solution to “all” the steps when “completed.” When we select “Get Help >> Solution >>Get Code Solution” we are provided the “final” solution not the intermediate solutions. Having the final and not intermediate solutions is problematic since we usually are correct in our intermediate step but just provide ‘more’ code than the step ‘requires.’
Make Sense?
Academic High Five
~Rick

1 Like

That makes sense now, thank you!

1 Like

I think here is a kind of mistake. In instruction 1 we have to create a three column table - id, year, temperature. The final solution expects the table to be with two columns only - year and temperature. No way to pass if you don’t give up and see the solution.

i guess that the id column will be filled automatically by the dbms since it is the primary key. It will just increment it by 1 for every row you add to that table.

this lecture is a bit off, since it requires you to write erroneous code to pass from step 3 onward. If you actually put the

averageTemperatureByYear.forEach(elem => {
            db.run....

into the correct position in the first place (that is in the callback to the CREATE statement) it wont let you move on. Then you have to check the “solution” to see what you should have been doing wrong…

I’m confused on step 1 for this exercise. When I looked at the answer I saw that the first db.run() had a second parameter of logNodeError. What does this mean?