About node.js operation MySQL Database related introduction Please read the full text. The details below are described below, as follows:
Installing the MySQL module
Database Preparation
MySQL server is located in the machine IP address is 192.168.0.108, login account with root@123456
Create test database in MySQL
Create a list of users 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: ' zhangsan@gmail.com '};
Connection.query (' insert into the users set? ', USR, function (err, result) {
if (err) throw err;
Console.log (' inserted Zhangsan ');
Console.log (result);
Console.log (' \ n ');
});
Update user, with conditions
Connection.query (' Update users set password= ddd ' where name= ' Zhangsan ', {password: ' PPP '}, function (err, result) {
if (err) throw err;
Console.log (' Updated zhangsan\ ' s password to DDD ');
Console.log (result);
Console.log (' \ n ');
Delete a user, with conditions
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 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 Complete
Complete functional description of MySQL module See 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: ' Pwdzhangsan ', Mail: ' zhangsan@gmail.com '}; Connection.query (' insert into the users set? ', USR, function (err, result) {if (err) throw err; Console.log (' Inserted Zhangsa
n ');
Console.log (result);
Console.log (' \ n ');
}); Connection.query (' SELECT * from users ', function (err, rows, fields) {if (err) throw err; Console.log (' selected after Inse
RTed '); 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 (E
RR) throw err;
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)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 the users where Name= ' Zhangsan ', {password: ' PPP '}, function (err, result) {if (err) throw err;
Console.log (' deleted Zhangsan ');
Console.log (result);
Console.log (' \ n ');
}); Connection.query (' SELECT * from users ', function (err, rows, fields) {if (err) throw err; Console.log (' selected after Dele
Ted '); for (var i= 0,usr;usr=rows[i++];) {console.log (' user nae= ' +usr.name + ', password= ' +usr.password);} console.log (' \ n ');})
; Connection.end ();