当前位置:网站首页>[node.js] MySQL module

[node.js] MySQL module

2022-06-26 13:24:00 Superman_ H

mysql modular

download mysql modular :npm install mysql

  1. introduce mysql modular :const mysql = require("mysql");
  2. Create connection objects
let connection = mysql.createConnection({
    
    host: 'localhost',
    user: 'root',
    password: '000930',
    database: 'xz'
});
  1. Create connection :connection.connect([callback]);
    callback: Receive an error parameter err

  2. disconnect :connection.end([callback])
    callback: Receive an error parameter err

demo

const mysql = require("mysql"); // 1. introduce  mysql  modular 

let connection = mysql.createConnection({
     // 2. Create connection objects 
    host: 'localhost',
    user: 'root',
    password: '000930',
    database: 'xz'
});

connection.connect(err => {
     // 3. Create connection 
    if (err) throw err;
    console.log(" Successfully connected ");
});

connection.end(err => {
     // 4. disconnect 
    if (err) throw err;
    console.log(" disconnect ")
});

operation mysql

  • stay connection.connect() and connection.end() Can be right between mysql Data to operate
  • Operation grammar :connection.query(sql[, sqlParams], callback)
    callback: receive 2 Parameters : error message err、 Execution results result

Inquire about

let sql = 'select * from xz_laptop_family';
connection.query(sql, (err, result) => {
    
    if (err) return err;
    console.log(result);
});

Insert

  • insert into Table name values( value 1, value 2);: Insert entire line , Write as many data as there are columns , It can't be less
  • insert into Table name ( Field 1, Field 2) values( value 1, value 2);: Insert the specified field
let sql = 'insert into xz_laptop_family values(null, " millet ")';
connection.query(sql, (err, result) => {
    
    if (err) throw err;
    console.log(result);
});

modify

  • update Table name set Field 1= value 1, Field 2= value 2 where Conditions ;
let sql = 'update xz_laptop_family set fname = "superPhone" where fname = " millet "';
connection.query(sql, (err, result) => {
    
    if (err) throw err.message;
    console.log(result);
});

Delete

let sql = 'delete from xz_laptop_family where fname="superPhone"';
connection.query(sql, (err, result) => {
    
    if (err) throw err;
    console.log(result);
});

Place holder ?

  • Prevent users from entering extra characters , Query for superfluous information

  • One ? Indicated value
    Two ?? Said column names 、 Table name

  • Single placeholder → You can write data directly
    Multiple placeholders → Need to be Array form Pass in multiple data

Inquire about demo

let sql = 'select * from ??'; //  Single placeholder 
let sqlParams = 'xz_laptop_family';
connection.query(sql, sqlParams, (err, result) => {
    
    if (err) return err;
    console.log(result);
});

Insert demo

  • insert into Table name set Field 1= value 1, Field 2= value 2;
  • sql Statements use set, Fit placeholder ?, Then, data in the form of objects is passed in , You can write less column values , But the name must not be misspelled
let sql = 'insert into xz_laptop_family set ?'; //  Single placeholder 
let sqlParams = {
    
    fname: " rice "
};
connection.query(sql, sqlParams, (err, result) => {
    
    if (err) throw err;
    console.log(result);
});

modify demo

  • sql Statements use set, Fit placeholder ?, Then, data in the form of objects is passed in , You can't write your name wrong
let sql = 'update ?? set ? where ?? = ?'; //  Multiple placeholders 
let sqlParams = ["xz_laptop_family", {
    
    fname: " Huawei ",
}, "fname", " rice "];
connection.query(sql, sqlParams, (err, result) => {
    
    if (err) throw err.message;
    console.log(result);
});

Delete demo

let sql = 'delete from ?? where ?? = ?'; //  Multiple placeholders 
let sqlParams = ["xz_laptop_family", "fname", " Huawei "];
connection.query(sql, sqlParams, (err, result) => {
    
    if (err) throw err;
    console.log(result);
});

Connection pool

The role of connection pooling is to manage connections , Improve performance . Because if you simply connect , Frequent opening and closing of connections is also a burden on the database . After using connection pool technology , You don't need to create a connection every time , Create multiple database connections at once , Put in a collection , Such a collection of database connection objects becomes a connection pool

download mysql modular :npm install mysql

  1. introduce mysql modular :const mysql = require("mysql");
  2. Create connection pool object
let pool = mysql.createPool({
    
    host: "127.0.0.1",
    port: "3306",
    user: "root",
    password: "000930",
    database: "xz",
    connectionLimit: 10 //  Connection pool size , The default is  15
});
  1. Remove the connection from the connection pool :pool.getConnection((err, connection) => {});
    If no connection is available , Then a database connection is implicitly established

  2. Close connection pool :pool.end([callback]);
    callback: Receive an error parameter err

  • When the connection is no longer in use , use connection Object's release Method to return it to the connection pool :
    connection.release();
  • When a connection is no longer needed and needs to be removed from the connection pool , use connection Object's destroy Method
    connection.destroy();

demo

const mysql = require('mysql');

let pool = mysql.createPool({
    
    host: "127.0.0.1",
    port: "3306",
    user: "root",
    password: "000930",
    database: "xz",
    connectionLimit: 10
});

pool.getConnection((err, connection) => {
    
    if (err) throw err;
    console.log(" Successfully connected ");
    console.log(pool._allConnections.length); // 1

    connection.query('select * from ??', "xz_laptop_family", function (err, result) {
    
        if (err) throw err
        console.log(result);
        console.log(pool._allConnections.length); // 0
    });

    pool.end(err => {
    
        if (err) throw err;
        console.log(" disconnect ")
    });
});

The connection pool can also be used directly connection Use

const mysql = require('mysql');

let pool = mysql.createPool({
    
    host: "127.0.0.1",
    port: "3306",
    user: "root",
    password: "000930",
    database: "xz",
    connectionLimit: 10
});

pool.query('select * from ??', "xz_laptop_family", function (err, result) {
    
    if (err) throw err
    console.log(result);
});
原网站

版权声明
本文为[Superman_ H]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170514231623.html