当前位置:网站首页>[node.js] MySQL module
[node.js] MySQL module
2022-06-26 13:24:00 【Superman_ H】
mysql modular
download mysql modular :
npm install mysql
- introduce mysql modular :
const mysql = require("mysql"); - Create connection objects
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '000930',
database: 'xz'
});
Create connection :
connection.connect([callback]);callback: Receive an error parameter errdisconnect :
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()andconnection.end()Can be right between mysql Data to operate - Operation grammar :
connection.query(sql[, sqlParams], callback)callback: receive 2 Parameters : error messageerr、 Execution resultsresult
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 lessinsert 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 nameSingle 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
- introduce mysql modular :
const mysql = require("mysql"); - 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
});
Remove the connection from the connection pool :
pool.getConnection((err, connection) => {});
If no connection is available , Then a database connection is implicitly establishedClose 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);
});
边栏推荐
- Appearance mode (facade)
- 7-3 minimum toll
- Beifu twincat3 can read and write CSV and txt files
- Mysql database explanation (IV)
- May product upgrade observation station
- 8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
- Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
- MediaPipe手势(Hands)
- Hdu1724[Simpson formula for integral]ellipse
- 12 SQL optimization schemes summarized by old drivers (very practical)
猜你喜欢

Opencv high speed download

ES基於Snapshot(快照)的數據備份和還原

There are many contents in the widget, so it is a good scheme to support scrolling

Beifu PLC realizes data power-off maintenance based on cx5130

利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)

Prototype

MySQL explanation (II)

Processsing mouse interactive learning

Mysql database explanation (III)

To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
随机推荐
sed编辑器
MongoDB系列之适用场景和不适用场景
[how to connect the network] Chapter 2 (middle): sending a network packet
H - Sumsets POJ 2229
Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
MySQL explanation (II)
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Es6: iterator
Script - crawl the customized storage path of the cartoon and download it to the local
Arcpy -- use of insertlayer() function: adding layers to map documents
2、并行接口、协议和相关芯片介绍(8080、8060)
Mysql database explanation (IV)
Enjoy element mode (flyweight)
7-1 n queen problem
MySQL数据库常见故障——遗忘数据库密码
LAMP编译安装
Sed editor
HDU 3555 Bomb
装饰器(Decorator)