Table of contents
②Logical deletion (recommended)
1. Install the mysql module
npm i mysql
2. Import and configure mysql
const mysql = require('mysql')
const db = mysql.createPool({
host:'127.0.0.1', //数据库ip地址
user:'root', //数据库账号
password:'123456',//数据库密码
database:'test_01'//数据库名称
})
db.getConnection(function (err, connection) {
if (err) {
throw new Error(err.message);
} else {
console.log("建立连接成功");
console.log(db._allConnections.length); // 1
connection.query("select 1", function (err, results) {
if(err) throw new Error(err.message) //失败返回错误信息
console.log(results) // 成功返回[ RowDataPacket { '1': 1 } ]
connection.destory(); // 当一个连接不再需要使用且需要从连接池中移除时,用connection对象的destroy方法
console.log(db._allConnections.length); // 0
});
}
db.end(); // 当一个连接池不再需要使用时,用连接池对象的end方法关闭连接池
});
Configuration parameters:
host |
Host address (default: localhost) |
user |
username |
password |
password |
port |
Port number (default: 3306) |
database |
Database name |
charset |
Connection character set (default: 'UTF8_GENERAL_CI', note that the letters in the character set must be uppercase) |
localAddress |
This IP is used for TCP connections (optional) |
socketPath |
Path to connect to unix domain, ignored when using host and port |
timezone |
Time zone (default: 'local') |
connectTimeout |
Connection timeout (default: no limit; unit: milliseconds) |
stringifyObjects |
Whether to serialize the object |
typeCast |
Whether to convert column values into local JavaScript type values (default: true) |
queryFormat |
Custom query statement formatting method |
supportBigNumbers |
When the database supports bigint or decimal type columns, this option needs to be set to true (default: false) |
bigNumberStrings |
supportBigNumbers and bigNumberStrings enable forcing bigint or decimal columns to be returned as JavaScript string types (default: false) |
dateStrings |
Force timestamp, datetime, and data types to be returned as string types instead of JavaScript Date types (default: false) |
debug |
Enable debugging (default: false) |
multipleStatements |
Whether to allow multiple MySQL statements in one query (default: false) |
flags |
Used to modify the connection flag |
ssl |
Use the ssl parameter (in the same format as the crypto.createCredenitals parameter) or a string containing the name of the ssl profile. Currently only Amazon RDS profiles are bundled. |
3. Database crud operation
1. Insert operation
const param = { name: "zhangsan", gender: "男", age: "20" };
const sqlStr = "insert into user (name,gender,age) values (?,?,?)";
db.query(sqlStr, [param.name, param.gender, param.age], (err, res) => {
if (err) throw new Error(err.message);
if (res.affectedRows == 1) {
console.log("插入成功");
}
});
2. Query operation
const param = {name:"zhangsan"}
const sqlStr = "select * from user where name = ?";
db.query(sqlStr, [param.name], (err, res) => {
if (err) throw new Error(err.message);
if (res) {
console.log(res);
//[ RowDataPacket { id: 1, name: 'zhangsan', gender: '男', age: '20' } ]
}
});
3.Update operation
const param = { name: "lisi", age: "23", id: 1 };
const sqlStr = "update user set name = ?,age = ? where id = ?";
db.query(sqlStr, [param.name, param.age, param.id], (err, res) => {
if (err) throw new Error(err.message);
if (res.affectedRows == 1) {
console.log("更新成功");
}
});
4. Delete operation
①Physical deletion
const param = { id: 1 };
const delSql = 'delete from user where id = ?';
db.query(delSql, [param.id], (err, res) => {
if (err) throw new Error(err.message);
if (res.affectedRows == 1) {
console.log("删除成功");
}
});
②Logical deletion (recommended)
const param = { status: 0 ,id: 1 };
const delSql = 'update user set status = ? where id = ?';
db.query(delSql, [param.status,param.id], (err, res) => {
if (err) throw new Error(err.message);
if (res.affectedRows == 1) {
console.log("删除成功");
}
});