mysql - 如何使用 NodeJS 在 MySQL 数据库表中插入行
问题描述
谁能让我知道这个查询有什么问题?
db.query(
'INSERT INTO permission (programId,create,read,update,delete) VALUES(' +
db.escape(ProgramId) +
',' +
db.escape(CreateSwitchedData) +
',' +
db.escape(ReadSwitchedData) +
',' +
db.escape(UpdateSwitchedData) +
',' +
db.escape(DeleteSwitchedData) +
') ',
(err, row) => {
if (!err) {
res.json('Permission Added Successfully!');
} else {
console.log(err);
}
}
);
我收到此错误:
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 'create,read,update,delete) VALUES(2,1,1,1,1)' at line " +
'1',
sqlState: '42000',
index: 0,
sql: 'INSERT INTO permission ' +
'(programId,create,read,update,delete) ' +
'VALUES(2,1,1,1,1) '
ProgramId 是从另一个表中引用的,但是键是正确的。请提供任何帮助。
解决方案
看起来您在查询中使用了关键字,而没有将它们引用为真实的。
INSERT INTO permission (programId,`create`,`read`,`update`,`delete`)
这可能是您要查找的内容,请使用反引号指定列名。