MySQL NodeJS准备语句插入或更新表名?



下面是我要做的代码:

connection.query({
sql: 'CREATE TABLE ? ( `wage` FLOAT NOT NULL , `monday` FLOAT NOT NULL , `tuesday` FLOAT NOT NULL , `wednesday` FLOAT NOT NULL , `thursday` FLOAT NOT NULL , `friday`) ENGINE = InnoDB;',
timeout: 40000, // 40s
},
//[arg1],
function (error, results, fields) {
if (error) {
console.log("Table creation failed");
}else{
console.log("Table creation success");
}
}
);

这不是一个准备好的语句,因为它把它放在引号里,这意味着语句看起来像这样:

CREATE TABLE 'test' ( `wage` FLOAT NOT NULL , `monday` FLOAT NOT NULL , `tuesday` FLOAT NOT NULL , `wednesday` FLOAT NOT NULL , `thursday` FLOAT NOT NULL , `friday`) ENGINE = InnoDB;

无效

所以我不得不写这样的语句:

connection.query({
sql: 'CREATE TABLE '+arg1+' ( `wage` FLOAT NOT NULL , `monday` FLOAT NOT NULL , `tuesday` FLOAT NOT NULL , `wednesday` FLOAT NOT NULL , `thursday` FLOAT NOT NULL , `friday` FLOAT NOT NULL) ENGINE = InnoDB;',
timeout: 40000, // 40s
},
//[''],
function (error, results, fields) {
if (error) {
console.log("Table creation failed");
}else{
console.log("Table creation success");
}
}
);

这工作,但它现在打开我的SQL注入,这是我试图避免的。

有什么方法可以解决这个问题并使用准备好的语句吗?

https://www.npmjs.com/package/mysql -这是我使用的包

可以使用" ?? "转义查询标识符或connection.escapeId().

https://www.npmjs.com/package/mysql escaping-query-identifiers

最新更新