Node+MySQL开发增删改查

//db.js
var mysql = require('mysql');
var pool = mysql.createPool({//创建连接池
  host:"localhost",
  user:"root",
  password:"root",
  database:"db_yd",
  port:"3306"
});
function query(sql,data,callback){
  pool.getConnection(function(err,connection){//创建连接
    connection.query(sql,data,function(err,rows){
      callback(err,rows);
      connection.release();//释放连接
    })
  })
}
exports.query = query;
//dbMapping.js
var user = {    
  insert:'INSERT INTO t_user(id, name, password) VALUES(0,?,?)',              
  update:'update t_user set name=?, age=? where id=?',    
  delete: 'delete  from t_user where id=?',    
  queryById: 'select * from t_user where id=?',    
  queryAll: 'select * from t_user'
};
module.exports = user;
//查
var db = require('db.js');
var user = require('user.js');
router.get('/',funtion(res,req,next){
  db.query(user.queryAll,[],function(err,rows){
    if(err)res.render(users,{title:"xxx",datas:[]});
    eles
       res.render({'users',{title:"xxx",datas:rows}});
  });
});

你可能感兴趣的:(Node+MySQL开发增删改查)