var mysql = require('mysql');
exports.query = function (sql, callBack) {var connection = mysql.createConnection({
host:'localhost',
Port:'3306',
user:'root',
password: 'rootadmin',
database:'test'
});
connection.connect();
connection.query(sql,function (err, results) {
if (err){
console.log('操作失败');
return;
}
callBack(err,results);
});
connection.end();
};
注意读取书的过程都是异步的。因此需要用到回调
还需要将函数暴露出去
【使用mvc对数据操控(封装)】调用包。调用函数。对数据进行操控
var db = require('./models/db.js');
db.query('select * from test',function (err, results) {
if (err){
console.log('失败');
return;
}
console.log(results);
});
db.query('select name from test',function (err, results) {
if (err){
console.log('失败');
return;
}
console.log(results);
});