var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: '------',
database: 'lijunyang'
});
connection.connect();
var addSql = 'INSERT INTO user (userName,password,Telephone, email, createTime,updatePasswordTime) VALUES(?,?,?,?,?,?)';
var addSqlParams = ['ccc', '777', '18979361605', '275120399@qq.com', new Date(), new Date()];
// 方法是异步的
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');
});
var modSql = 'UPDATE user SET userName = ? WHERE userId = ?';
var modSqlParams = ['111', '8'];
// 方法是异步的
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');
});
var delSql = 'DELETE FROM user where userId = 10 or userId = 12';
connection.query(delSql, function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.query('select * from user', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.end();
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + Object.getOwnPropertyNames(connection));
});
connection.query('show databases', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.query('show tables', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results);
});