我正在使用NodeJs msyql,
var telemetryItemList = [
{ 'created_at': createdAt, 'device_type': deviceType + '1', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '2', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '3', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '4', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '5', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue }
];
pool.getConnection((err, conn) => {
if(err) {
throw new Error('Could not get DB connection');
}
conn.query(`INSERT INTO ${Config.tableName} SET ?`, telemetryItemList, (err, res) => {
.
.
.
上面的代码仅插入列表的第一行,为什么?
更新
在我在这里收到很好的答案之后,我相信最好将整个对象刷新到数据库中,而不要在每一行上创建查询
类似的东西:
var telemetryItemList2 = [
['created_at': createdAt, 'device_type': deviceType + '1', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue],
['created_at': createdAt, 'device_type': deviceType + '2', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue],
['created_at': createdAt, 'device_type': deviceType + '3', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue],
['created_at': createdAt, 'device_type': deviceType + '4', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue],
['created_at': createdAt, 'device_type': deviceType + '5', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue],
];
但是我该如何构建模型?
答案 0 :(得分:0)
var telemetryItemList = [
{ 'created_at': createdAt, 'device_type': deviceType + '1', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '2', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '3', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '4', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue },
{ 'created_at': createdAt, 'device_type': deviceType + '5', 'device_serial': deviceSerial , 'metric_name': metricName, 'metric_value': metricValue }
];
尝试遍历telemetryItemList
:
pool.getConnection((err, conn) => {
if(err) {
throw new Error('Could not get DB connection');
}
telemetryItemList.forEach(row => {
conn.query(`INSERT INTO ${Config.tableName} SET ?`, row, (err, res) => {
//
});
});
});
答案 1 :(得分:0)
您需要使用INSERT ... VALUES
,(INSERT ... SET
没有批量支持),并且还需要将telemetryItemList
设置为array:
pool.getConnection((err, conn) => {
if(err) {
throw new Error('Could not get DB connection');
}
conn.query(`INSERT INTO ${Config.tableName} (created_at, device_type, device_serial, metric_name, metric_value) VALUES ?`, [telemetryItemList.map(tel_arr => [tel_arr.created_at, tel_arr.device_type, tel_arr.device_serial, tel_arr.metric_name, tel_arr.metric_value])], (err, res) => {
.
.
.