mysql - 如何在 NodeJS 中使用变量插入查询
问题描述
我有一个查询,但我不确定如何允许 NodeJS 使用变量输入它。我该如何实现?什么是最好的方法?
var sql = (
"INSERT INTO stats_tmp (
id,
timestamp,
campaign_nm,
calls,
seconds,
answered,
failure,
dnc,
amd,
transfers,
transfer_seconds,
cost
) VALUES ("
+ estarr[0].id + ", "
+ estarr[0].timestamp + ", "
+ estarr[0].name + ", "
+ estarr[1].calls + ", "
+ estarr[1].seconds + ", "
+ estarr[1].answers + ", "
+ estarr[1].failures + ", "
+ estarr[1].dncs + ", "
+ estarr[1].amd + ", "
+ estarr[1].transfers + ", "
+ estarr[1].transfers + ", "
+ estarr[1].transferseconds + ", "
+ estarr[1].cost
+ ")"
);
解决方案
您想使用准备好的语句。
考虑:
var query = connection.query(
'INSERT INTO stats_tmp (
id,
timestamp,
campaign_nm,
calls,
seconds,
answered,
failure,
dnc,
amd,
transfers,
transfer_seconds,
cost
) VALUES (
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)',
[
estarr[0].id,
estarr[0].timestamp,
estarr[0].name,
estarr[1].calls,
estarr[1].seconds,
estarr[1].answers,
estarr[1].failures,
estarr[1].dncs,
estarr[1].amd,
estarr[1].transfers,
estarr[1].transfers,
estarr[1].transferseconds,
estarr[1].cost
],
function(err, results) {
...
});
推荐阅读
- javascript - 如何在vuejs中为输入字段设置值
- z3 - 在 smt/z3 优化中哪个非线性断言是完整的?
- ruby-on-rails - 如何使用 Ruby shopify_api gem 创建 GraphQL appSubscriptionCreate 突变?
- javascript - TypeError:无法读取 reactjs 登录页面中未定义错误的属性“令牌”
- flutter - 如何动画包装小部件
- promise - BeforeUpload 不会在 promise 已解决时触发上传
- php - 如何在 laravel 中生成 Url
- php - 如何在php中使用浏览器中的url登录?
- postgresql - 删除odoo 12中的日记账计数器数据
- python - 如何将整个字符串转换为列表中的一项,而不是每项一个字符?