Node.js operation MySQL database, delete and check _node.js

Source: Internet
Author: User

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.