Dev321
Dev321

Reputation: 11

MySQL Syntax error in the update statement, NodeJS(MySQL2) module

I am using Node.JS along with the mysql2 module. It's basically like when I try to update a column with a JSON stingified data, I get the following error:

{ Error: You have an error in your SQL syntax; check the manual that corresponds
 to your MariaDB server version for the right syntax to use near '"1050":1}WHERE
 `user` = ?' at line 1

The stingified JSON data:

{"1050":1}

The query:

var sql = 'UPDATE `users` SET `furniture` = ' + 'concat(furniture,' + JSON.stringify(self.furniture) + ')' + 'WHERE `user` = ?';

self.furniture is related to something else, but I can assure you that self.furniture is returning that JSON data thus I get the mysql syntax error.

sqlMessage: 'You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax to use near \'"1050":

Upvotes: 0

Views: 538

Answers (2)

Dev321
Dev321

Reputation: 11

Issue is resolved using backticks along with single quotes.

    var sql = 'UPDATE `users` SET `furniture` = ' + `concat(furniture, '${lol}')` + 'WHERE `user` = ?';

    var lol = JSON.stringify(self.furniture)

Upvotes: 1

O. Jones
O. Jones

Reputation: 108706

Your query line says in part

 ...ingify(self.furniture) + ')' + 'WHERE `us...

It should have an extra space after the close parenthesis.

...ingify(self.furniture) + ') ' + 'WHERE `us...

Here's the thing about MySQL syntax errors: the message shows the erroneous query starting with the first character MySQL does not understand. A good way to troubleshoot this kind of thing is to use console.log() to output the entire query string, then look at it carefully. You will usually find something obvious wrong.

Upvotes: 0

Related Questions