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