社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

javascript mysql插入多个值

user829174 • 4 年前 • 305 次点击  

我正在尝试将接收到的request.body结构插入数据库

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"
    }
]

正在尝试将request.body设置到数据库中:

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 // <- Here it the problem, I guess

conn.query(sql, [values], function(err) {
    if (err) throw err;
    conn.end();
});

我在试着理解如何构建 var values 因此它将符合以下格式:

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'],
];
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51089
 
305 次点击  
文章 [ 3 ]  |  最新文章 4 年前