const mysql = require('mysql');
const uuidV1 = require('uuid/v1');
const pool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'root',
database : 'test'
});
pool.getConnection(function(err, connection) {
// 使用连接
//新增
const addSql = 'INSERT INTO user(id,name,age) VALUES(?,?,?)';
const addSqlParams = [uuidV1(),'张三', 18];
connection.query(addSql,addSqlParams,function (err, result) {
if(err){
console.log('[INSERT ERROR] - ',err.message);
return;
}
console.log('--------------------------INSERT----------------------------');
console.log('INSERT ID:',result.insertId);
console.log('INSERT ID:',result);
console.log('-----------------------------------------------------------------\n\n');
});
//修改
const modSql = 'UPDATE user SET name = ?,age = ? WHERE Id = ?';
const modSqlParams = ['李四', '22', '98a4c6a1-923f-11e7-b765-0b7a0e39a928'];
connection.query(modSql,modSqlParams,function (err, result) {
if(err){
console.log('[UPDATE ERROR] - ',err.message);
return;
}
console.log('--------------------------UPDATE----------------------------');
console.log('UPDATE affectedRows',result.affectedRows);
console.log('-----------------------------------------------------------------\n\n');
});
//删除
const delSql = 'DELETE FROM user where id="68501de0-9241-11e7-bd3a-fd8e5cf24a25"';
connection.query(delSql,function (err, result) {
if(err){
console.log('[DELETE ERROR] - ',err.message);
return;
}
console.log('--------------------------DELETE----------------------------');
console.log('DELETE affectedRows',result.affectedRows);
console.log('-----------------------------------------------------------------\n\n');
});
//查询
connection.query( 'SELECT * FROM user', function(err, rows) {
// 使用连接执行查询
console.log(rows);
connection.release();
//连接不再使用,返回到连接池
});
});
网友评论