目前我有这个问题,问题是表名得到一组引号(ad它是一个字符串),这会使服务器崩溃。
const update = 'the name of my column';
const UpdateQuery = `UPDATE scores
SET ${mysql.escape(update)} = ${mysql.escape(newValue)}
WHERE score_id = ${mysql.escape(singleScore.score_id)}`;mysql.escape()对除列名以外的所有内容都工作得很好。
如果我在注入变量后对查询执行console.log,则会得到以下结果:
UPDATE scores
SET 'the name of my column' = 1
WHERE score_id = 1发布于 2021-04-15 06:10:36
看起来您正在使用mysql NPM package。
escape方法用于转义查询值。要转义查询标识符(如列名),应该改用escapeId方法。您的代码应如下所示:
const update = 'the name of my column';
const UpdateQuery = `UPDATE scores
SET ${mysql.escapeId(update)} = ${mysql.escape(newValue)}
WHERE score_id = ${mysql.escape(singleScore.score_id)}`;同样,如果使用替换,请使用双问号而不是单问号来转义标识符。
const update = 'the name of my column';
const UpdateQuery = `UPDATE scores
SET ?? = ?
WHERE score_id = ?`;
const replacements = [update, newValue, singleScore.score_id];发布于 2018-09-20 23:50:18
Tamilvanan解决方案只需稍加更改即可解决此问题
db.query(
'UPDATE scores SET '+update+' = ? Where score_id = ?',
[newValue, singleScore.score_id],
(err, result) => {
if (err) throw err;
console.log(`Changed ${result.changedRows} row(s)`);
}
);发布于 2018-09-20 23:35:32
对于奇怪的MySQL列名,不能用单引号将它们括起来。单引号只是将值转换为字符串。
在MySQL中,反引号用于此操作。例如
UPDATE `table with space` SET `column with space` = 'bar';https://stackoverflow.com/questions/52428398
复制相似问题