How to encapsulate the MySQL database in node

Source: Internet
Author: User

1. Install the XAMPP integrated environment locally



2. Introduce the installed MySQL module in the Mysql.js

Const MySQL = require ("MySQL");//Initialize Const CONF = {  host: "localhost",  User: "root",  password: "",  Port: "3306",  //Use which table  database: "Test"};
3. Use a variable to save the retrieved database information and perform a series of operations

Save information for connected database  Const CON = mysql.createconnection (conf);  Start connecting  con.connect ();  Perform database operation  con.query (SQL, (err, result) = = {    callback (err, result)  });  End  Con.end ();

4. Use a function to encapsulate the method of data manipulation and share it

NOTE: Here the SQL parameter is the SQL statement for the corresponding operation.

function fn (SQL, callback) {  //save information for connected database  Const CON = mysql.createconnection (conf);  Start connecting  con.connect ();  Perform database operation  con.query (SQL, (err, result) = = {    callback (err, result)  });  End  con.end ();} Module.exports = fn;

  

5. Introduce the Mysql.js file in other files, directly use the variable to carry out the database of reference operation

Const SQL = require ("./mysql.js");

  

How to encapsulate the MySQL database in node

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.