Installing the MySQL module
NPM install MySQL
Database Preparation
MySQL server is located on the machine IP address is 192.168.0.108, login account with [email protected]
Creating the test database in MySQL
Create a users table in the test database
Operation
Connecting to a database
var mysql=require (' MySQL '); var connection = mysql.createconnection ({ host ' 192.168.0.108 ', user ' Root ', ' 123456 ', ' test1 ', port:' 3306 '}); Connection.connect ( );
Insert a user
var usr={name: ' Zhangsan ', Password: ' Pwdzhangsan ', mail: ' [email protected] '};connection.query ( function (err, result) { ifthrow err; Console.log (' inserted Zhangsan '); Console.log (result); Console.log (' \ n ');});
Update user, with conditional
function (err, result) { ifthrow err; Console.log (' updated zhangsan\ ' password to DDD '); Console.log (result); Console.log (' \ n ');});
Delete a user, with conditional
Connection.query (' Delete from function(err, result) { ifthrow err; Console.log (' deleted Zhangsan '); Console.log (result); Console.log (' \ n ');});
Query user, all
function (Err, rows, fields) { ifthrow err; Console.log (' selected after deleted '); for (var i= 0,usr;usr=rows[i++];) { console.log (' user nae= ' +usr.name + ', password= ' + Usr.password); } Console.log (' \ n ');});
To close a database connection
Connection.end ();
Basic CRUD Completion
The full functionality of the MySQL module is described in the official:
Https://www.npmjs.com/package/mysql
Https://github.com/felixge/node-mysql
All demo Code
varMysql=require (' MySQL ');varConnection =mysql.createconnection ({host:' 192.168.0.108 ', User:' Root ', Password:' 123456 ', Database:' Test1 ', Port:' 3306 '}); Connection.connect ();varUsr={name: ' Zhangsan ', Password: ' Pwdzhangsan ', mail: ' [email protected] '};connection.query (' INSERT into users set? ', USR,function(err, result) {if(ERR)Throwerr; Console.log (' Inserted Zhangsan '); Console.log (result); Console.log (' \ n ');}); Connection.query (' SELECT * from users ',function(Err, rows, fields) {if(ERR)Throwerr; Console.log (' Selected after inserted '); for(varI= 0,usr;usr=rows[i++];) {console.log (' User nae= ' +usr.name + ', password= ' +Usr.password); } console.log (' \ n ');}); Connection.query (' Update users set password= ' DDD ' where name= ' Zhangsan ', {password: ' PPP '},function(err, result) {if(ERR)Throwerr; Console.log (' Updated zhangsan\ ' s password to DDD '); Console.log (result); Console.log (' \ n ');}); Connection.query (' SELECT * from users ',function(Err, rows, fields) {if(ERR)Throwerr; Console.log (' Selected after updated '); for(varI= 0,usr;usr=rows[i++];) {console.log (' User nae= ' +usr.name + ', password= ' +Usr.password); } console.log (' \ n ');}); Connection.query (' Delete from users where name= ' Zhangsan ', {password: ' PPP '},function(err, result) {if(ERR)Throwerr; Console.log (' Deleted Zhangsan '); Console.log (result); Console.log (' \ n ');}); Connection.query (' SELECT * from users ',function(Err, rows, fields) {if(ERR)Throwerr; Console.log (' Selected after deleted '); for(varI= 0,usr;usr=rows[i++];) {console.log (' User nae= ' +usr.name + ', password= ' +Usr.password); } console.log (' \ n ');}); Connection.end ();
node. JS operation MySQL database increase and deletion check