简体   繁体   中英

invalid syntax on mysql : nodejs

Newbie to nodejs. I am thrown an invalid syntax error. This is probably coming up from trying to escape characters.

let i = 0;
while(i < answered.length){
    let td = [
        connection.escape(questionnaireId),
        connection.escape(sId),
        connection.escape(questionId),
        connection.escape(answered[i]),
        connection.escape(hutk)
    ];
    values.push(td);
    i++;
}

let sql = "INSERT INTO answers (qId, sId, key, value, hutk) VALUES ?";
connection.query(sql, [values], function(err, result){
    if(err) throw err;
    console.log("Successfull Insert");
    connection.release();
});

I get

"errno": 1064, "sqlMessage": "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 'key, value, hutk) VALUES ('18', '\\'yZfXqYnvIXmLMuDAthbRr7IHtkFp5cl2ZSPCsEwY\\'', ' at line 1", "sqlState": "42000", "index": 0,

 "sql": "INSERT INTO answers (qId, sId, key, value, hutk) VALUES ('18', '\\\\'yZfXqYnvIXmLMuDAthbRr7IHtkFp5cl2ZSPCsEwY\\\\'', '5','\\\\'23@sd.lk\\\\'', '\\\\'f98a7585ce8d984a902df90263cdab5d\\\\'')" 

Key a reserved Word in mysql Please try

 let sql = "INSERT INTO answers (qId, sId, `key`, value, hutk) VALUES ?";

You could put around all Columns, but this works too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM