Tuesday, 9 May 2017

Updating rows in MySQL using Node.js

I'm trying to update results in MySQL using Nodejs. I'm getting results back from an API, and updating the database based on entrieId.

My code is the following:

    const array = response.data.map(item => [hours, item.id]);
    const query = connection.query('UPDATE entries SET hours = ? WHERE entrieId = ?', [array],
        (error, results, fields) => {
            if (error) console.log(error);
            else {
                console.log('Updated!');
            }
        });

If I console.log(array) it shows that my data has come back as:

[[ 1.5, 42434515 ],
[ 2, 42434552 ],
[ 1, 43223831 ]]

I am having success inserting data, so the database connection and data is working fine, it's just the UPDATE query I'm having the issue with.

This is giving me the following error:

You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use 
near '(1.9166666666666667, 63708), (2, 393835), (2, 393829), (2.75
, 398982), (2.75, 40' at line 1

I am using the above from posts by other people who had the same issue, however their suggested solutions are not working for me. Any ideas?



via Jim Dover

No comments:

Post a Comment