Skip to content

Instantly share code, notes, and snippets.

@hzhopen
Created March 22, 2014 01:41
Show Gist options
  • Save hzhopen/9699882 to your computer and use it in GitHub Desktop.
Save hzhopen/9699882 to your computer and use it in GitHub Desktop.
NodeJS连接Mysql函数封装
NodeJS连接Mysql函数封装
var mysql = require('mysql');
//填写数据库连接信息,可查询数据库详情页
var username = 'username';
var password = 'passwork';
var db_host = '127.0.0.1';
var db_port = 3306;
var db_name = 'test';
var option = {
host: db_host,
port: db_port,
user: username,
password: password,
database: db_name
};
function _exec(sqls,values,after) {
var client = mysql.createConnection(option);
client.connect(function(err){
if (err) {
console.log(err);
return;
}
client.query(sqls || '', values || [],function(err,r){
after(err,r);
});
client.end();
});
client.on('error',function(err) {
if (err.errno != 'ECONNRESET') {
after("err01",false);
throw err;
} else {
after("err02",false);
}
});
}
exports.exec = _exec;
node-mysql库:https://github.com/felixge/node-mysql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment