Original address: https://www.cnblogs.com/kongxianghai/p/5335632.html
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 ', password: ' 123456 ', database: ' test1 ', port: ' 3306 '}, Connection.connect ();
Insert a user
var usr={name: ' Zhangsan ', Password: ' Pwdzhangsan ', mail: ' [email protected] '};connection.query (' insert into users set? ') , USR, function (err, result) { if (err) throw err; Console.log (' inserted Zhangsan '); Console.log (result); Console.log (' \ n ');});
Update user, with conditional
Connection.query (' Update users set password= ' DDD ' where name= ' Zhangsan ', {password: ' PPP '}, function (err, result) { if (err) throw err; Console.log (' Updated zhangsan\ ' password to DDD '); Console.log (result); Console.log (' \ n ');});
Delete a user, with conditional
Connection.query (' Delete from users where name= ' Zhangsan ', {password: ' PPP '}, function (err, result) { if (err) throw err; Console.log (' deleted Zhangsan '); Console.log (result); Console.log (' \ n ');});
Query user, all
Connection.query (' select * from the users ', function (err, rows, fields) { if (err) throw 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
var mysql=require (' MySQL '); var connection = Mysql.createconnection ({host: ' 192.168.0.108 ', User: ' Root ', Password: ' 123456 ', database: ' Test1 ', Port: ' 3306 '}, Connection.connect (); var usr={name: ' Zhangsan ', Password: ' PW Dzhangsan ', Mail: ' [email protected] '};connection.query (' insert into users set? ', USR, function (err, result) {if ( ERR) throw err; Console.log (' inserted Zhangsan '); Console.log (result); Console.log (' \ n ');}); Connection.query (' select * from the users ', function (err, rows, fields) {if (err) throw err; Console.log (' selected after inserted '); for (var i= 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) throw err; Console.log (' Updated zhangsan\ ' password to DDD '); Console.log (result); Console.log (' \ n ');}); Connection.query ('SELECT * from the users ', function (err, rows, fields) {if (err) throw err; Console.log (' selected after updated '); for (var i= 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) throw E Rr Console.log (' deleted Zhangsan '); Console.log (result); Console.log (' \ n ');}); Connection.query (' select * from the users ', function (err, rows, fields) {if (err) throw 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 ');}); Connection.end ();
Another example of reference: JS operation database to achieve registration and login
node. JS operation MySQL database increase and deletion check