【发布时间】:2021-12-06 11:33:58
【问题描述】:
我有一个用 Express 和 MariaDB 作为 DBMS 编写的 API,我正在尝试使用数组同时更新多条记录,但它不起作用。 示例:
const data = [ [ '2', '130' ], [ '4', '10' ] ]
try {
const updatedInventory = await pool.query('UPDATE inventory SET qty_shipped = qty_shipped + VALUES ? WHERE sku_id = VALUES ?', [data])
res.status(201).json(updatedInventory)
} catch (error) {
res.status(500).json({ message: error.message })
console.error(error.message)
}
但是我得到了这个错误:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'VALUES ('2', '130'), ('4', '10') WHERE sku_id = VALUES ?' at line 1
我对插入端点做了类似的事情,我也在使用数组进行批量插入,效果很好:
const newInventory = await pool.query('INSERT INTO inventory (sku_id, qty_received, expiration_date) VALUES ? ON DUPLICATE KEY UPDATE qty_received = qty_received + VALUES(qty_received) ', [data])
关于如何使批量更新部分正常工作的任何想法?
【问题讨论】:
标签: node.js express mariadb pool bulkupdate