Use node && mysql together

Source: Internet
Author: User

This article is based on a simple combination of development on Mac, node && MySQL.

I. Building the Environment

    • Download the relevant version of your computer to the official website. Then start MySQL

    • When using NPM-V to detect whether the computer installed Nodejs, if not on the node website to download the appropriate version to install, is the version installed on the machine

    • Build your project with NPM, install Express and MySQL, and execute npm install express MySQL--save in the created directory
    • The express here is temporarily unused and can be used without installation

II. Structure of the project

This project is a simple combination of node and MySQL, so the structure is relatively simple, as follows

"Digression, I use the editor is VSC"

Iii. combined use of Node&mysql

    • Execute the relevant code into the database and create the database, I created the test database as follows

    • Enter the following code in the project that you created:
Here is the introduction of the database var mysql = require (' mysql ');//Create link var connection = mysql.createconnection ({    host: ' localhost ',    User: ' root ',    password: ' YES ', database    : ' Reng '}//Start link connection.connect ();//Operate the database Connection.query (' SELECT * from students ', function (err,rows,fields) {    if (err) {        console.log (err);    }    if (rows) {for        (var i =0; i<rows.length;i++) {            console.log (rows[i].name+ '-to ' +rows[i].age);}}    ) Disconnect link connection.end ();

Iv. observation Results

Execute node app.js in the corresponding folder, and you will see the following results

  

"Results are consistent with expectations"

"Original, quote please specify the source, thank you"

Use node && mysql together

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.