查询工作在MySQL Workbench上,但在node.js中是语法返回问题
我需要一些帮助,当我在工作台中执行此查询时,它的工作正常
INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500);
INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),'work perfect');
但是当我在nodejs中做相同的事情时
const createNewReport = () => {
return new Promise((resolve, reject) => {
connection.query(
`INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500);
INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),'work perfect');`,
(err, result) => {
if (err) reject(err);
resolve(result);
}
);
});
};
我收到此错误:
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 \'INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),\'work perfe\' at line 2',
sqlState: '42000',
index: 0,
sql:
'INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500);\n INSERT INTO reports_tax_data(reportID,data) VALUES (LAST_INSERT_ID(),\'work perfect\');' }
但是当我进行普通选择时,请在nodejs中选择其工作
回答如下:我在这里假设您正在使用的Node SQL API不允许每个调用执行一个以上的语句(对于其他一些编程语言确实是这种情况)。尝试为每个插入单独拨打电话:
connection.query(
`INSERT INTO reports(creatorUserID,currentUserEditorID,companyID,stageID,scenarioID,typeID,year) VALUES (1,1,456545,1,1,1,1500)`,
(err, result) => {
if (err) reject(err);
resolve(result);
}
);
并且对reports_tax_data
表插入执行相同操作。