You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js向MySQL插入数据时出现ER_WRONG_VALUE_COUNT_ON_ROW错误求助

问题原因与解决方案

错误原因

从报错的SQL语句可以看到:VALUES ('0x34916740ed6e957e1d6f34eF60225685dBf5ca5f'),这里只传入了1个值,但你指定了10个列,导致列数和值数不匹配,触发ER_WRONG_VALUE_COUNT_ON_ROW错误。

问题出在Node.js代码的SQL参数传递方式:你使用了VALUES (?),mysql模块会将单个?替换为数组的第一个元素,不会自动展开整个数组的10个值。

修复方法

有两种可行的修复方式:

方式一:使用嵌套数组包裹参数

将参数数组放在一个外层数组里,让mysql模块把整个嵌套数组解析为一行的多个值:

app.post("/prescribe-patient",(req,res)=>{
    console.log("Prescribing prescription to patient")
    console.log("The request is", req.body.doctor_public_address)
    const params = [
        req.body.doctor_public_address,
        req.body.drug_name,
        req.body.drug_manufacturer, 
        req.body.patient_age,
        req.body.practice_id,
        req.body.drug_dosage,
        req.body.drug_frequency,
        req.body.doctor_name,
        req.body.doctor_license_number,
        req.body.patient_address
    ];
    db.query(
        "INSERT into prescription (doctor_public_address,drug_name,drug_manufacturer,patient_age,practice_id,drug_dosage,drug_frequency,doctor_name,doctor_license_number,patient_address) VALUES (?)",
        [params],
        (err,result)=>{
            if(err){
                res.send(err)
            }else{
                res.send(result)
            }
        }
    )
})

方式二:对应列数使用多个占位符?

在SQL语句中为每个列都添加一个?,直接传递参数数组:

app.post("/prescribe-patient",(req,res)=>{
    console.log("Prescribing prescription to patient")
    console.log("The request is", req.body.doctor_public_address)
    db.query(
        "INSERT into prescription (doctor_public_address,drug_name,drug_manufacturer,patient_age,practice_id,drug_dosage,drug_frequency,doctor_name,doctor_license_number,patient_address) VALUES (?,?,?,?,?,?,?,?,?,?)",
        [req.body.doctor_public_address,req.body.drug_name,req.body.drug_manufacturer, req.body.patient_age,req.body.practice_id,req.body.drug_dosage,req.body.drug_frequency,req.body.doctor_name,req.body.doctor_license_number,req.body.patient_address],
        (err,result)=>{
            if(err){
                res.send(err)
            }else{
                res.send(result)
            }
        }
    )
})

验证说明

修复后生成的SQL会包含10个对应的值,和列数匹配,就能正常执行插入操作。

内容的提问来源于stack exchange,提问作者Khandkar Islam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 15:55:27