Nodejs Operation MySQL

Source: Internet
Author: User

Work every morning have more than half an hour mornings, in addition to each of the tasks, the rest is to share, for just into the industry of the rookie to know nothing, looking for technology to learn, and then share, really tired, but also a progress it

Nodejs operates MySQL, most of it is in the blog park to learn someone else's code, without understanding very thoroughly, just to share

First: Configure the Nodej environment and related packages, this way is mainly the MYSQ package, via the command npm install MySQL installation

Second: MySQL database operation

Create Data: Test database

To create a table in this database:

  

Then you can write the code:

Introduction Package

var mysql = require ('mysql');  

Create a connection

Create Connection  var client = mysql.createconnection ({    User: ' root ',    password: ' Root ',  

Connect to MySQL

Client.connect (function (err) {    if(err)    {        console.log ('  Erroer');    }    Console.log ('succeed');});

Which database is linked

var ' Test ' // db client.query ("" + database);

Inquire

//Enquiryvarsql ='SELECT * from Usertest'; client.query (SQL, function (err, results, fields) {if(err) {Throwerr; }        if(results) {//Enquiry              for(vari =0; i < results.length; i++) {Console.log ("This ID is"+results[i].uid); Console.log ("This name is"+results[i].uname); Console.log (" This is"+results[i].uage); Console.log ("This sex is"+results[i].usex); Console.log ("This Tel is"+Results[i].utel); }        }    });

Similarly: Insert, modify, delete, etc. can be done this way

But there are other ways to insert it.

'Set uid =?, uname =?, Uage =?, Usex =?, Utel =? ' , [3'sv',1,'  13897686543'

Turn off data connections

// Close Connection client.end (function (err) {    if(err)    {        console.log ('erroer  ');    }    Console.log ('succeed');});

The database is simple to operate, not the best

Nodejs Operation MySQL

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.