const mysql = require('mysql')
const pool = mysql.createPool({
connectionLimit : 10,
host : '127.0.0.1',
user : 'root',
password : '123456',
database : 'my_database'
})
let db= function( sql, values ) {
return new Promise(( resolve, reject ) => {
pool.getConnection(function(err, connection) {
if (err) {
reject( err )
} else {
connection.query(sql, values, ( err, rows) => {
if ( err ) {
reject( err )
} else {
resolve( rows )
}
connection.release()//释放连接池
})
}
})
})
}
module.exports = db
网友评论