js mysql insert 操作,javascript mysql insert multiple values

这篇博客探讨了如何在 Node.js 中使用 'node-mysql' 库将接收到的 JSON 数据批量插入 MySQL 数据库。示例中,请求体包含多个设备指标数据,博主尝试将这些数据转换为适合 SQL 插入语句的格式。问题在于正确构造 `values` 变量以匹配 SQL 的问号占位符。解决方案涉及将 JSON 对象数组转换为二维数组。
摘要由CSDN通过智能技术生成

I am trying to insert the request.body I receive which is struct the following into database

request.body = [{

"createdAt": "2019-03-11T08:03:11.438",

"deviceType": "type1",

"deviceSerial": "123",

"metricName": "metric1",

"metricValue": "29"

},

{

"createdAt": "2019-03-11T08:03:11.442",

"deviceType": "type2",

"deviceSerial": "234",

"metricName": "metric2",

"metricValue": "29"

},

{

"createdAt": "2019-03-11T08:03:11.442",

"deviceType": "type3",

"deviceSerial": "345",

"metricName": "metric3",

"metricValue": "165"

}

]

Trying to set the request.body into database:

var mysql = require('node-mysql');

var conn = mysql.createConnection({

...

});

var sql = "INSERT INTO Test (created_at, device_type, devices_serial, metric_name, metric_value) VALUES ?";

var values = request.body //

conn.query(sql, [values], function(err) {

if (err) throw err;

conn.end();

});

I am trying to understand how to construct the var values so it will fit to the following format:

var values = [

['2019-03-11T08:03:11.438', 'type1', '123', 'metric1', '29'],

['2019-03-11T08:03:11.438', 'type2', '234', 'metric2', '29'],

['2019-03-11T08:03:11.438', 'type3', '345', 'metric3', '165'],

];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值