当前位置:网站首页>[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);
});
边栏推荐
- Mediapipe gestures (hands)
- B - Bridging signals
- Electron official docs series: Examples
- 2. Introduction to parallel interface, protocol and related chips (8080, 8060)
- ES中索引别名(alias)的到底有什么用
- ES基于Snapshot(快照)的数据备份和还原
- Es sauvegarde et restauration des données par instantané
- I - Dollar Dayz
- Appearance mode (facade)
- A few lines of code can realize complex excel import and export. This tool class is really powerful!
猜你喜欢
[how to connect the network] Chapter 2 (middle): sending a network packet
MongoDB系列之Window环境部署配置
Learn how to develop owl components by hand (7): practical use of owl projects
防火墙介绍
装饰器(Decorator)
MySQL explanation (I)
Electron official docs series: Get Started
12 SQL optimization schemes summarized by old drivers (very practical)
Firewall introduction
Awk tools
随机推荐
泰山OFFICE技术讲座:使用字体粗体的四种情形
First pass! Baidu AI Cloud Xiling platform has obtained the authoritative certification of digital human ability evaluation from the Institute of information technology
A primary multithreaded server model
ES6:Map
MariaDB study notes
Script - crawl the customized storage path of the cartoon and download it to the local
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
sed编辑器
Adapter mode
MediaPipe手势(Hands)
Solutions to insufficient display permissions of find and Du -sh
Beifu twincat3 can read and write CSV and txt files
HDU 3555 Bomb
三维向量的夹角
MySQL数据库常见故障——遗忘数据库密码
C language: Exercise 2
防火墙介绍
Mysql database explanation (III)
8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)