Skip to content
Advertisement

Node-MySQL unable to insert TIME

I am trying to build an API in Express.JS with an MySQL-Server running in the background. One of the tables contains a column with a type of TIME. When trying to insert into that table however, I get {code: ‘ER_PARSE_ERROR’, errno: 1064, sqlState: ‘42000’}

Using a single ?, the message is:

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 ”23:00:41” at line 1

Using a double ??, the message is:

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 ‘`23:00:41`’ at line 1

The actual lines of code look like that:

let query = "INSERT INTO Zeiterfassung (Break_Start) VALUES ? ";
let data = Break_Start; //Break_Start is a string like "23:40:39"
dbConn.query(query, data, (err, rows) => {...});

MySQL-Version: 8.0.22-0ubuntu0.20.04.2

Node-Version: v10.19.0

Advertisement

Answer

If you have a string parameter, then you need to surround the ? with parentheses:

let query = "INSERT INTO Zeiterfassung (Break_Start) VALUES (?)";
dbConn.query(query, [ Break_Start ], function(err, rows) => {...});

The syntax with a single ? without parentheses is meant to pass objects.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement