node. JS operation MySQL database increase and deletion check

Source: Internet
Author: User

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

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.