1. var mysql = require('mysql');
    2. var connection = mysql.createConnection({
    3. host: '127.0.0.1',
    4. user: 'root',
    5. password: '------',
    6. database: 'lijunyang'
    7. });
    8. connection.connect();
    9. var addSql = 'INSERT INTO user (userName,password,Telephone, email, createTime,updatePasswordTime) VALUES(?,?,?,?,?,?)';
    10. var addSqlParams = ['ccc', '777', '18979361605', '275120399@qq.com', new Date(), new Date()];
    11. // 方法是异步的
    12. connection.query(addSql, addSqlParams, function (err, result) {
    13. if (err) {
    14. console.log('[INSERT ERROR] - ', err.message);
    15. return;
    16. }
    17. console.log('--------------------------INSERT----------------------------');
    18. //console.log('INSERT ID:',result.insertId);
    19. console.log('INSERT ID:', result);
    20. console.log('-----------------------------------------------------------------\n\n');
    21. });
    22. var modSql = 'UPDATE user SET userName = ? WHERE userId = ?';
    23. var modSqlParams = ['111', '8'];
    24. // 方法是异步的
    25. connection.query(modSql,modSqlParams,function (err, result) {
    26. if(err){
    27. console.log('[UPDATE ERROR] - ',err.message);
    28. return;
    29. }
    30. console.log('--------------------------UPDATE----------------------------');
    31. console.log('UPDATE affectedRows',result.affectedRows);
    32. console.log('-----------------------------------------------------------------\n\n');
    33. });
    34. var delSql = 'DELETE FROM user where userId = 10 or userId = 12';
    35. connection.query(delSql, function (error, results, fields) {
    36. if (error) throw error;
    37. console.log('The solution is: ', results);
    38. });
    39. connection.query('select * from user', function (error, results, fields) {
    40. if (error) throw error;
    41. console.log('The solution is: ', results);
    42. });
    43. connection.end();
    1. connection.connect(function(err) {
    2. if (err) {
    3. console.error('error connecting: ' + err.stack);
    4. return;
    5. }
    6. console.log('connected as id ' + Object.getOwnPropertyNames(connection));
    7. });
    8. connection.query('show databases', function (error, results, fields) {
    9. if (error) throw error;
    10. console.log('The solution is: ', results);
    11. });
    12. connection.query('show tables', function (error, results, fields) {
    13. if (error) throw error;
    14. console.log('The solution is: ', results);
    15. });