在使用node.js,为公司开发功能的时候所遇到的,其中一个表名中的某个字段为保留字,导致无法插入,在desc不能修改为其他命名的时候解决如下:
const into = `INSERT INTO cms_article (title, cate_id, competition_id, content, md_content, create_time,admin_id,is_home,thumb,desc) VALUES ('${item.tit}', '${sqlobj.classification.id}', '${resleague[0].id}', '${item.content}', '', '${item.time}',1,1,${item.thumb},'啊啊啊啊啊')`;
报错:
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 'desc) VALUES
原因:因为desc在sql语句中是保留字
解决:
const into = `INSERT INTO cms_article (title, cate_id, competition_id, content, md_content, create_time,admin_id,is_home,thumb,\`desc\`) VALUES ('${item.tit}', '${sqlobj.classification.id}', '${resleague[0].id}', '${item.content}', '', '${item.time}',1,1,${item.thumb},'啊啊啊啊啊')`;
desc 修改为 \`desc\`
在navicat中解析为
错误的:
正确的: