TransWikia.com

Node-MySQL unable to insert TIME

Stack Overflow Asked by niclassic on December 15, 2020

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

One 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.

Correct answer by GMB on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP