1

I'm trying to create a new course row into my Courses database in postman and I cannot get the syntax correct.

I've got 159 values that need to be added into a single course. Any chance I can summarise them instead of having to write down all the values?

Currently this is my query:

const addCourse = "INSERT INTO courses VALUES (value1 'value2','value3', 'value4', 'value5', 'value6', 'value7', 'value8', 'value9', 'value10', 'value11', 'value12', 'value13', 'value14', 'value15', 'value16', 'value17', 'value18', 'value19', 'value20', 'value21', 'value22', 'value23', 'value24', 'value25', 'value26', 'value27', 'value28', 'value29', 'value30', 'value31', 'value32', 'value33', 'value34', 'value35', 'value36', 'value37', 'value38', 'value39', 'value40', 'value41', 'value42', 'value43', 'value44', 'value45', 'value46', 'value47', 'value48', 'value49', 'value50', 'value51', 'value52', 'value53', 'value54', 'value55', 'value56', 'value57', 'value58', 'value59', 'value60', 'value61', 'value62', 'value63', 'value64', 'value65', 'value66', 'value67', 'value68', 'value69', 'value70', 'value71', 'value72', 'value73', 'value74', 'value75', 'value76', 'value77', 'value78', 'value79', 'value80', 'value81', 'value82', 'value83', 'value84', 'value85', 'value86', 'value87', 'value88', 'value89', 'value90', 'value91', 'value92', 'value93', 'value94', 'value95', 'value96', 'value97', 'value98', 'value99', 'value100', 'value101', 'value102', 'value103', 'value104', 'value105', 'value106', 'value107', 'value108', 'value109', 'value110', 'value111', 'value112', 'value113', 'value114', 'value115', 'value116', 'value117', 'value118', 'value119', 'value120', 'value121', 'value122', 'value123', 'value124', 'value125', 'value126', 'value127', 'value128', 'value129', 'value130', 'value131', 'value132', 'value133', 'value134', 'value135', 'value136', 'value137', 'value138', 'value139', 'value140', 'value141', 'value142', 'value143', 'value144', 'value145', 'value146', 'value147', 'value148', 'value149', 'value150', 'value151', 'value152', 'value153', 'value154', 'value155', 'value156', 'value157 ', 'value158' )"

This is my courseController code:

const addCourse = (req, res) => {
    console.log(req.body);
    const { id_curso } = req.body;
    //check Curso exists
    pool.query(queries.checkIdCursoExists, [id_curso], (error, results) => {
        if (results.rows.length) {
            res.send("Este curso ja existe.");
        }
        // add course
        pool.query(queries.addCourse),
            (error, results) => {
                if (error) throw error;
                res.status(201).send("Curso criado com sucesso");
            };
    });
};

The problem I encounter is this error message whether I have value1 in quotes or not:

error: type "value1" does not exist'

The course is not posted onto my database.

4
  • 2
    There are quotes missing around the value1 in your code, and a comma missing after it. Commented Dec 29, 2022 at 13:02
  • I've changed it and now I get this error message: error: invalid input syntax for type integer: "value1" value 1 should be a string as it's the ID of the course and it's a mix of letters and numbers Commented Dec 29, 2022 at 13:08
  • 2
    The string 'value1' is not an integer. If the first value must be an integer, write something like INSERT INTO courses VALUES (123, 'value2', ...). Commented Dec 29, 2022 at 13:09
  • 1
    Welcome to Stack Overflow. Please edit your question to show us the definition of your table, with all its columns. Commented Dec 29, 2022 at 13:25

1 Answer 1

1

The path to your answer lies in your INSERT statement. I guess your courses table has 159 columns in it. (That is a great many columns and may suggest the need to normalize your table. SQL handles multiple-row data more efficiently than multiple-column data where it makes sense to do so. But you did not ask about that.)

The INSERT syntax is either this:

INSERT INTO tbl (col1, col2, col3) VALUES (const, const, const);

or this:

INSERT INTO tbl VALUES (const, const, const);

The first syntax allows you to insert a row without giving values for every column in the table. You use the second syntax. It requires you to give one constant value for each column of your table. But your insert looks like this:

INSERT INTO courses VALUES (value1 'value2', ... , 'value158' )"

I see some problems with this.

  1. You only have 158 values, but your question says you have 159.
  2. value1, the first value in your list, isn't a constant.
  3. You need a comma after your first value.
  4. All your value constants are text strings. Yet you mentioned float in the title of your question.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.