【问题标题】:How to insert json data into mysql table using nodejs with json data如何使用带有json数据的nodejs将json数据插入mysql表
【发布时间】:2019-11-30 04:48:01
【问题描述】:

ER_WRONG_VALUE_COUNT_ON_ROW:列计数与第 1 行的值计数不匹配

我在下面的代码中显示是否需要发送表的所有列。

  let rawdata = fs.readFileSync(uploadPath);
        try {
            student = JSON.parse(rawdata);
           // emp = student.employee;
           gstr2recon=student.cptyRecons;
           finPeriod=student.finPeriod;
      //console.log('gstr2recon',gstr2recon);

      jsonData['finperiod']=student['finperiod'];
      jsonData['cname']=student.cptyRecons['cname'];
      jsonData['total']=student.cptyRecons['totalCnt'];
      jsonData['matched']=student.cptyRecons['matchedCnt'];
      jsonData['mismatched']=student.cptyRecons['mismatchedCnt'];
      jsonData['onhold']=student.cptyRecons['onHoldCnt'];
      jsonData['pendinginasp']=student.cptyRecons['pendingCnt'];
      jsonData['availingstn']=student.cptyRecons['avlGstnCnt'];
      jsonData['email']=student.cptyRecons['cemail'];


           var database = mysql.createConnection({
            host:'192.168.1.1',
            port:'3306',
            user:'root',
            password:'password',
            database:'ui_support'
           });
           database.connect(function(connectionError){
            if(connectionError){
              throw connectionError;
            }
            console.log('jsonData',jsonData);
            var sql = "INSERT INTO gstr2Recon(finperiod,cname,ctin,total,matched,mismatched,onhold,pendinginasp,availingstn,email) VALUES ('" + jsonData + "')";
            database.query(sql, function(queryError, queryResult){
              if(queryError){
                throw queryError;
              }
            });
          });

我需要插入一些具有匹配 json 键值的列数据。

【问题讨论】:

  • 您必须解析 JSON 并单独设置每个字段。
  • 如果一个对象里面有一个复杂的数组,那么做这种方法会很困难。在Java中我们直接存储对象。我们有没有将文本数据转换为对象并保存在数据库中的过程.

标签: mysql node.js ejs


【解决方案1】:

它的拼写错误

jsonData['finperiod']=student['finPeriod'];

请您在分配时遵循列名顺序和拼写错误。

【讨论】:

    猜你喜欢
    • 2022-08-14
    • 2017-04-17
    • 2015-01-09
    • 2020-09-13
    • 2015-12-13
    • 1970-01-01
    • 2018-05-31
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多