node. JS operation MySQL database increase and deletion check

Source: Internet
Author: User

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

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.