【发布时间】:2021-07-11 17:21:29
【问题描述】:
我有以下包含少量 sql 查询的代码:
createNewOrder: (data,callBack) => {
pool.query(
`Insert into ordering (supplycontract) values (?)`,
[
data.supplierId
],
(error,results,fields) => {
if(error){
return callBack(error)
}
pool.query(
`select id from ordering order by id desc limit 1`,
[
data.articleId
],
(error,results1,fields) =>{
if(error){
return callBack(error)
}
let orderid = results1[0].id;
console.log(orderid);
pool.query(
`Insert into position (quantity,delivery,ordering,positioninorder,articlecontract) values(?,?,?,1,?)`,
[
data.quantity,
new Date(),
orderid,
data.articleId
],
(error,results3,fields) => {
if(error){
callBack(error)
}
//callBack(null,results3)
}
);
}
);
return callBack(null,results)
}
);
}
我尝试了几种技巧并改进了代码,但不幸的是我收到了以下错误:
Error: ER_PARSE_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
'position (quantity,delivery,ordering,positioninorder,articlecontract) values(54,' at line 14
code: 'ER_PARSE_ERROR',
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 'position (quantity,delivery,ordering,positioninorder,articlecontract) values(54,' at line 1",
sqlState: '42000',
index: 0,
sql: "Insert into position (quantity,delivery,ordering,positioninorder,articlecontract) values(54,'2021-07-11 19:18:39.334',16,1,5)"
}
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:518:11)
code: 'ERR_HTTP_HEADERS_SENT'
【问题讨论】:
-
return callBack(null, results)应该在最后一次查询的回调函数中 -
谢谢!这帮助我解决了 HTTP_Headers 的第二个错误,但仍然存在 SQL 语法错误。代码:'ER_PARSE_ERROR',errno:1064。不过,这些查询对我来说似乎没问题。
-
SELECT VERSION();在您的数据库服务器上返回什么?我在 MySQL 8.0.23 实例上测试了您的插入语句。它没有报告任何语法错误(它确实给出了错误,因为我没有名为 position 的表,但这意味着它至少通过了语法检查)。 -
我的版本是 MySQL 8.0.25。
标签: mysql node.js mysql-error-1064