node连接mysql

var mysql=require("mysql");
var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'test1',
    port: 3306
});
var query = function(sql,callback){

    pool.getConnection(function(err,connection){
            
            if(err){
                callback(err,null,null);
            }else{
            connection.query(sql, function (error, results, fields) {           //执行查询语句
                 connection.release();      //
                 callback(err,results,fields);
            });
        }

    });
    // conn.end();
};
module.exports = query;

 

posted @ 2017-05-12 14:51  webbky  阅读(114)  评论(0编辑  收藏  举报