如何使用MySQL在Node.js中创建挂起状态和成功状态

时间:2018-07-09 05:44:38

标签: mysql node.js

我是nodejs的新手。我有“出售”,“购买”和“ liveorderbook”表。我将数据插入到买卖表中之后,买卖表中匹配的数据将转到liveorderbook表中。将数据插入到买卖表后,如果买卖表数据匹配,则显示“待处理状态”,我需要在liveorderbook表中设置“成功状态”,以及如何在买卖表中将“待处理状态”设置为不匹配的数据。这是我的代码

var express = require('express');
var bodyParser = require('body-parser');
var ejs = require('ejs');
var app = express();
var port = 3000;
var mysql = require('mysql');
const http = require('http');
var router = express.Router();
var orders = require('./routes/orders'); 

var connection  = require('express-myconnection');

app.set('view engine','ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
var connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "orderbook"
});
app.get('/',function(req,res){
  res.render('index');

});

app.get('/orders',function(req,res){


   var sql = "SELECT volume , price ,total FROM sell ";
   connection.query(sql, function (err, result , fields) {
     if (err)     { 
       console.log("error", err) 
     } else {
       res.render('orders', {data:result})     
        } 
   });

});

app.get('/buy',function(req,res){


  var sql = "SELECT volume , price ,total FROM buy ";
  connection.query(sql, function (err, result , fields) {
   // console.log('chandu',err,result,fields);
    if (err)     { 

      console.log("error", err) 
    } else {
      res.render('buy', {data:result})     
       } 
  });

});


app.get('/liveorderbook',function(req,res){
  var Query = 'select b.volume, b.price, b.total from buy as b, sell as s  where b.price = s.price and s.volume = b.volume';
  connection.query(Query, function(err,responce){
     console.log('chandu',err,responce)
 // res.render('liveorderbook',{data:responce});

  if(responce&&responce.length>0){
    for (var i = 0; i < responce.length; i++) {
      var v=responce[i].volume;
      var p=responce[i].price;
      var t=responce[i].total;
    var sql='INSERT INTO liveorderbook (volume,price,total) SELECT * FROM (SELECT "'+v+'","'+p+'","'+t+'") AS tmp WHERE NOT EXISTS (SELECT volume,price,total FROM liveorderbook WHERE volume ="'+v+'",price= "'+p+'",total="'+t+'")';
     connection.query(sql,(err1,res1)=>{
         if (err1){
           console.log(typeof(err1));
           console.log('data not inserted');
         }else{
          console.log(typeof(res1));
           console.log('data inserted');
         }
     })
    }
  }
  res.render('liveorderbook',{data:responce});
  });
 //res.render('liveorderbook',{data:res});
});



app.post('/sell',function(req,res){
  //Check this log in console, it should print all the values properly, if not issue is at clinet side
  console.log('Input:: name='+req.body.box4+' email='+ req.body.box5 +' password='+ req.body.box6,   req.body);

  var Query = 'INSERT INTO sell (volume,price,total) VALUES ("'+req.body.box4+'","'+req.body.box5+'","'+req.body.box6+'" )';


  connection.query(Query, function(err,res){
      if(err) {
        console.error('Error in sql query:' + Query + ' Err', err);
        throw err;
      }
  });
  res.redirect('/');
});

app.post('/buy',function(req,res){
  //Check this log in console, it should print all the values properly, if not issue is at clinet side
  console.log('Input:: name='+req.body.box1+' email='+ req.body.box2 +' password='+ req.body.box3,   req.body);

  var Query = 'INSERT INTO buy (volume,price,total) VALUES ("'+req.body.box1+'","'+req.body.box2+'","'+req.body.box3+'" )';


  connection.query(Query, function(err,res){
      if(err) {
        console.error('Error in sql query:' + Query + ' Err', err);
        throw err;
      }
  });
  res.redirect('/');
});




app.listen(port, () => {
    console.log("Server listening on port " + port);
  });

下面给出了出售表图片:enter image description here

0 个答案:

没有答案