【发布时间】:2021-08-01 10:09:11
【问题描述】:
我正在尝试使用 JavaScript 中的动态“WHERE”条件为 Amazon Athena 准备一些 SQL 查询。查询保存在另一个文件中的一个变量上。如何为其添加自定义 WHERE 条件?
我想要实现的伪代码,
file1.js
module.exports.queryFromFile1 = 'SELECT * Name FROM Contact WHERE Name = ?';
file2.js
const {queryFromFile1} = require('./file1.js');
const newQuery = queryFromFile1,['Jane'];
console.log(newQuery); // 'SELECT * Name FROM Contact WHERE Name = 'Jane'
任何人都可以向我建议一个正确的方法来做到这一点。我目前的解决方案发布在下面。
file1.js
module.exports.queryFromFile1 = 'SELECT * Name FROM Contact WHERE Name = {stringToReplace}';
file2.js
const {queryFromFile1} = require('./file1.js');
const newQuery = queryFromFile1.replace("{stringToReplace}", "'Jane");
console.log(newQuery); // 'SELECT * Name FROM Contact WHERE Name = 'Jane'
【问题讨论】:
-
请只使用准备好的语句。无需手动向SQL injection 敞开心扉
-
@VLAZ 感谢您的评论。你能解释一下准备好的陈述吗?我正在准备的查询不是针对 MYSQL/MSSQL,而是针对亚马逊 Athena。
-
嗨,@LawrenceCherone,我正在尝试向Amazon Athena 执行此查询,因此上述方法对我不起作用。
-
我猜你的意思是
SELECT * FROM Contact?
标签: javascript string interpolation