创建OptPool.js
var mysql = require('mysql');
function OptPool(){
this.flag = true;
this.pool = mysql.createPool({
host:'localhost',
user:'root',
password:'',
database:'mtc',
port:'3306'
});
this.getPool = function(){
if (this.flag) {
this.pool.on('connection',function(connection){
connection.query('SET SESSION auto_increment_increment=1');
this.flag = false;
});
}
return this.pool;
}
}
module.exports = OptPool;
创建mysqlPool.js
var OptPool = require('./models/OptPool');
var optPool = new OptPool();
var pool = optPool.getPool();
pool.getConnection(function(err,conn){
//删除
var deleteSql = "DELETE FROM mtc_users WHERE user_id = ? ";
var deleteParams = ['3'];
conn.query(deleteSql,deleteParams,function(err,rs){
if (err) {
console.log(err);
return;
}
console.log('delete success');
conn.release();
})
// //修改
// var updateSql = "UPDATE mtc_users SET user_pass = ? WHERE user_id = ? ";
// var updateParams = ['7891012','3'];
// conn.query(updateSql,updateParams,function(err,rs){
// if (err) {
// console.log(err);
// return;
// }
// console.log('update success');
// conn.release();
// })
// //查询
// var selectSql = "SELECT * FROM mtc_users";
// var selectParams = [];
// conn.query(selectSql,selectParams,function(err,rs){
// if (err) {
// console.log(err);
// return;
// }
// for(var i = 0;i<rs.length;i++){
// console.log(rs[i].user_name);
// }
// conn.release();
// })
// //插入
// var insertSql = "INSERT INTO mtc_users(user_name,user_pass,create_time) VALUES(?,?,?) ";
// var insertParams = ['zhaoyun','zhao123','123'];
// conn.query(insertSql,insertParams,function(err,rs){
// if (err) {
// console.log(err);
// return;
// }
// console.log('insert success');
// conn.release();
// })
});
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。