MySQL database – used by node

1 MySQL query object

2 MySQL query array

3 Introduction to the use of mysql2 library

4 mysql2 prepared statements

5 mysql2 connection pool usage

6 Promi for mysql2

Here we only explain how to use the server to connect to the database and perform operations.

A prepared statement is a statement that can input variables (the expression is signed:?). Need to use .execute to execute;

 

Need to run ordinary statements (statements without adding variables). Just use query.

Prepared statements have many benefits, such as good performance and security (SQL injection).

 If there are many connected users, creating and destroying database connections every time will have an impact, so we can use connection pools for optimization when creating database connections.

Connection method without connection pool:

 The method using the connection pool:

 

 

You need to download the corresponding third-party library to enable node to drive the database:

npm install mysql2

Prepare data - insert data from json file into database

Get the json format data from the phpne.json file and write it to the database.

const mysql = require('mysql2');
 
const connection = mysql.createConnection({
  host: 'localhost',
  port: 3306,
  user: 'root',
  password: 'Coderwhy123.',
  database: 'music_db'
});

const statement = `INSERT INTO products SET ?;`
const phoneJson = require('./phone.json');

for (let phone of phoneJson) {
  connection.query(statement, phone);
}

Contents of phone.jsond:

[
  {
    "brand": "华为",
    "title": "华为nova 3(全网通) ",
    "price": 2699,
    "score": 6.7,
    "voteCnt": 65,
    "url": "http://detail.zol.com.cn/cell_phone/index1185512.shtml",
    "pid": "1185512"
  },
  {
    "brand": "华为",
    "title": "华为P20 Pro(6GB RAM/全网通) ",
    "price": 4488,
    "score": 8.3,
    "voteCnt": 103,
    "url": "http://detail.zol.com.cn/cell_phone/index1207038.shtml",
    "pid": "1207038"
  },
  {
    "brand": "华为",
    "title": "华为P20(全网通) ",
    "price": 3388,
    "score": 8.4,
    "voteCnt": 127,
    "url": "http://detail.zol.com.cn/cell_phone/index1175779.shtml",
    "pid": "1175779"
  },
  {
    "brand": "华为",
    "title": "华为nova 3i(4GB RAM/全网通) ",
    "price": 1999,
    "score": 7,
    "voteCnt": 9,
    "url": "http://detail.zol.com.cn/cell_phone/index1222100.shtml",
    "pid": "1222100"
  }
]

mysql2-basic use

const mysql = require('mysql2')

// 1.创建一个连接(连接上数据库)
const connection = mysql.createConnection({
  host: 'localhost',
  port: 3306,
  database: 'music_db',
  user: 'root',
  password: 'Coderwhy123.'
})


// 2.执行操作语句, 操作数据库
const statement = 'SELECT * FROM `students`;'
// structure query language: DDL/DML/DQL/DCL
// query可以执行DDL/DML/DQL/DCL的语句的代码。返回的值在回调函数里面。
connection.query(statement, (err, values, fields) => {
  if (err) {
    console.log('查询失败:', err)
    return
  }

  // 查看结果
  console.log(values)
  // console.log(fields)
})

mysql2-prepared statements

const mysql = require('mysql2')

// 1.创建一个连接
const connection = mysql.createConnection({
  host: 'localhost',
  port: 3306,
  database: 'music_db',
  user: 'root',
  password: 'Coderwhy123.'
})

// 2.执行一个SQL语句: 预处理语句
const statement = 'SELECT * FROM `products` WHERE price > ? AND score > ?;'
connection.execute(statement, [1000, 8], (err, values) => {
  console.log(values)
})

// connection.destroy()

mysql2-connection pool usage

const mysql = require('mysql2')

// 1.创建一个连接
const connectionPool = mysql.createPool({
  host: 'localhost',
  port: 3306,
  database: 'music_db',
  user: 'root',
  password: 'Coderwhy123.',
 // connectionLimit用来限制连接数量的
  connectionLimit: 5
})

// 2.执行一个SQL语句: 预处理语句
const statement = 'SELECT * FROM `products` WHERE price > ? AND score > ?;'
connectionPool.execute(statement, [1000, 8], (err, values) => {
  console.log(values)
})

mysql2-Promise writing method

const mysql = require('mysql2')

// 1.创建一个连接
const connectionPool = mysql.createPool({
  host: 'localhost',
  port: 3306,
  database: 'music_db',
  user: 'root',
  password: 'Coderwhy123.',
  connectionLimit: 5
})

// 2.执行一个SQL语句: 预处理语句
const statement = 'SELECT * FROM `products` WHERE price > ? AND score > ?;'

connectionPool.promise().execute(statement, [1000, 9]).then((res) => {
  const [values, fields] = res
  console.log('-------------------values------------------')
  console.log(values)
  console.log('-------------------fields------------------')
  console.log(fields)
}).catch(err => {
  console.log(err)
})

Guess you like

Origin blog.csdn.net/weixin_56663198/article/details/131095781