【问题标题】:How to add table of values of html to mysql using node .js如何使用 node .js 将 html 的值表添加到 mysql
【发布时间】:2018-05-01 18:17:05
【问题描述】:

我正在做一个关于自动时间表生成系统的项目。我是 node .js 的新手。这是将值表从 html 插入到 mysql 的程序。当我运行这个程序时,它会导致死锁情况。

这是我的索引 .js 文件。

var express=require('express');
 var app=express();
 app.set('view engine','ejs');
 app.use(express.static('./public'));
 app.use(express.static("."));
 var Array=require('node-array');
 var mysql = require('mysql');
 var s=require('string');

 //Connection pool
 var pool=mysql.createPool({
 connectionLimit:10,
 host: "localhost",
 user: "root",
 password: "user@1235",
 database:"time_table"
 });

app.post('/expt2',urlencodedParser,function(req,res){
  var table=req.body.table1;
  console.log(req.body);

  var data=[];
  for(var key in (req.body)){
    data.push(req.body[key]);
  }
  for(i=0;i<(data.length-1);i++)
    console.log(data[i]);

  var i=0;
  while(i<(data.length-1))
  {
    lg=data[i];
    console.log(lg.substr(2,1));
    if(lg.substr(2,1)=="4")
    {   
      sem=4  ;  
      room="lh1"; 
    }
   else if(lg.substr(2,1)=="6")
   {
     sem=6;
     room="lh2";
   }
  else if(lg.substr(2,1)=="8")
  {
     sem=8;
     room="lh3";
  }
  else
  {
     sem=5;
     room="lh2";
  }

  day="Thrusday";
  console.log(room);
  console.log(sem);
  pool.getConnection(function(err,con){
    if (err) throw err;
    console.log("Connected!");
    console.log(lg);

    pool.query("select fid from course where 
    cid='"+lg+"'",function(err,result)
    {

     if(err)throw err;
     else 
     {
        console.log("okay");
        console.log(result);

        Object.keys(result).forEach(function(key){
        row=result[key];
        console.log(row.fid);
        ob=row.fid;
        console.log(ob);
        var sql="INSERT INTO classroom(sem,day,timings,fid,cid,room_name) 
        VALUES('"+sem+"','"+day+"','12:00:00','"+ob+"','"+cid+"','"+room+ 
        "');"

        con.query(sql,function(err,result)
        {

          if(err)throw err;
          console.log("one row inserted");
          //con.release();
        });
       });
   }
  });
  });
  i++;}
 });

这是 expt2.html 文件。

<html>
    <head>
      <title>Master-Timetable</title>
      <link rel="stylesheet"           
      href="/public/asset/css/bootstrap.min.css" type="text/css"/>
      <script type="text/javascript" src="/public/asset/popper.min.js"> 
      </script>
      <script src="/public/asset/js/bootstrap.min.js" 
      type="text/javascript"></script>
      <script src="/public/asset/jquery-3.2.1.min.js" 
      type="text/javascript"></script>

    </head>

    <body>
     <form id="contacth" method="POST" action="/expt2"></form>
      <table id="table1">
       <tr class="head">
        <th>Time->day|</th>
        <th>7:30-8:30</th>
        <th>8:30-9:30</th>
        <th>9:30-10:30</th>
        <th rowspan="19">BREAK</th>
        <th>11:00-12:00</th>
        <th>12:00-1:00</th>
        <th rowspan="19">LUNCH BREAK</th>
        <th>2:30-3:30</th>
        <th>3:30-4:30</th>
        <th>4:30-5:30</th>
      </tr>
      <tr>
       <th rowspan="3" class="parent" value="monday">Monday</th>
        <td class="child"><input type="text" maxlength="5" name="am1" 
        form="contacth"></td>
        <td ><input type="text" maxlength="5" name="am2" form="contacth"/> 
        </td>
        <td><input type="text" maxlength="5" name="am3" form="contacth"/> 
        </td>
        <td><input type="text" maxlength="5" name="am4" form="contacth"/> 
        </td>
        <td><input type="text" maxlength="5" name="am5" form="contacth"/> 
        </td>
        <td><input type="text" maxlength="5" name="am6" form="contacth"/> 
        </td>
        <td><input type="text" maxlength="5" name="am7" form="contacth"/> 
        </td>
        <td><input type="text" maxlength="5" name="am8" form="contacth"/> 
        </td>
      </tr>
      <tr>
       <td>
        <input type="submit" name="t_save" value="save" class="btn btn- 
        success" form="contacth"/><br/>
      </td>
     </tr>
   </table>
  </body>
 </html>

我收到了使用“async”和“await”的建议。但我并没有真正了解 在我的代码中使用“异步”并等待。

【问题讨论】:

  • 你可以通过模块化将你的业务逻辑放在一个函数中,你可以使用 async await 他们或者你可以使用 promise 来处理异步流。使用 pool.getConnection() 方法并获得一个连接和然后使用连接查询然后关闭连接

标签: javascript html mysql node.js node-mysql


【解决方案1】:

尽量不要直接查询池。将第一个 pool.query 替换为 con.query。

看到这个问题:Difference between using getConnection() and using pool directly in node.js with node-mysql module?

【讨论】:

  • 错误:ER_TRUNCATED_WRONG_VALUE_FOR_FIELD:不正确的整数值:Query.Sequence._packetToError (C:\sh\node_modules\mysql) 中第 1 行的列 'fid' 的整数值不正确\lib\protocol\sequences\Sequence.js:52:14) 在 Query.ErrorPacket (C:\sh\node_modules\mysql\lib\protocol\sequences\Query.js:77:18) 在 Protocol._parsePacket (C: \sh\node_modules\mysql\lib\protocol\Protocol.js:279:23)
  • 试试去掉+ob+两边的双引号
  • 看起来变量 ob 是一个字符串,而 fid 需要一个整数。确保为给定列传递适当的数据类型和值。
  • Ob 是一个整数变量。
  • 新错误 - ER_LOCK_DEADLOCK: 尝试获取锁时发现死锁;尝试在 Query 重新启动事务。 sequence._packetToError
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多