Nodejs连接mysql并实现增、删、改、查操作的方法详(3)

运行的结果如下

D:\User\myappejs4>node mysqltest_query.js
---------------SELECT----------------
[ { id: 1, name: 'admin', age: 32 },
 { id: 2, name: 'dans88', age: 45 },
 { id: 3, name: '张三', age: 35 },
 { id: 4, name: 'ABCDEF', age: 88 },
 { id: 5, name: '李小二', age: 65 },
 { id: 6, name: 'Wilson', age: 55 } ]
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

4、删除操作

var mysql = require('mysql');
var connection = mysql.createConnection({
 host   : '127.0.0.1',
 user   : 'root',
 password : 'root123',
 port: '3306',
 database: 'my_news_test',
});
connection.connect();
var userDelSql = 'DELETE FROM node_user WHERE id = 7';
//ɾ
connection.query(userDelSql,function (err, result) {
    if(err){
     console.log('[DELETE ERROR] - ',err.message);
     return;
    }
    console.log('-------------DELETE--------------');
    console.log('DELETE affectedRows',result.affectedRows);
    console.log('&&&&&&&&&&&&&&&&&');
});
connection.end();
运行的结果如下
D:\User\myappejs4>node mysqltest_del.js
-------------DELETE--------------
DELETE affectedRows 1
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

增、删、改、查操作应会部完成了!

希望本文所述对大家nodejs程序设计有所帮助。