【问题标题】:Node.js Update Table with Array in MysqlNode.js 在 Mysql 中使用数组更新表
【发布时间】:2021-04-08 07:36:09
【问题描述】:

我有这张桌子。

我的数据在下面,把这张表放在上面。

buffer= [{deviceId: 000002, input2:false, input3:false, input4:false, input5:false, input6:false, input7:false, input8:false, output1:false, output2:false, output3:false, output4:false, output5:false, output6:false, output7:false, output8:false},
{deviceId: 000001, input2:false, input3:false, input4:false, input5:false, input6:false, input7:false, input8:false, output1:false, output2:false, output3:false, output4:false, output5:false, output6:false, output7:false, output8:false}]

我想把我的数据放在这个查询中

dbConnect.query("update datas set input1=?, input2=?, input3=?, input4=?, input5=?, input6=?, input7=?, input8=?, output1=?, output2=?, output3=?, output4=?, output5=?, output6=?, output7=?, output8=? where deviceId=?", [buffer],
function (err, rows, fields) {
     if (!err) {
        console.log("OK")
       } else {
        console.log("ERROR")
    }
})

下面还有错误

sqlMessage:“您的 SQL 语法有错误;请查看手册 对应于您的 MySQL 服务器版本以获得正确的语法 在 ''000002' 附近使用,输入 2=?,输入 3=?,输入 4=?,输入 5=?,输入 6=?, input7=?, input8=?, ' at line 1", sqlState: '42000', index: 0,
sql: "更新数据集 input1='[object Object]', '000002', input2=?, input3=?, input4=?, input5=?, input6=?, input7=?, input8=?, output1=?, 输出2=?, 输出3=?, 输出4=?, 输出5=?, 输出6=?, 输出7=?, 输出8=? deviceId=?”

我需要你的帮助,谢谢。

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:

    我找到了我的问题的解决方案。需要使用

    insert into TABLENAME(key, column1, column2, ...) values (?),(?),... on duplicate key update column1=value(column1), column2=value(column2), ...;
    

    但是有一个细节,那就是key 列必须是unique index

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2011-06-16
      • 2021-12-10
      • 2017-02-05
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多