系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Mysql > 详细页面

MySQL数据库node使用详解

时间:2023-10-29来源:系统城装机大师作者:佚名

1 MySQL查询对象

2 MySQL查询数组

3 mysql2库介绍使用

4 mysql2预处理语句

5 mysql2连接池使用

6 mysql2的Promi

这里仅说明如何使用服务器连接数据库并进行操作。

预处理语句就是可以输入变量的语句(表现形式是有符号:?)。需要使用.execute来执行;

需要运行普通的语句(不添加变量的语句)。就使用query。

预处理语句有很多好处,比如性能好、安全性(sql注入)。

 如果连接的用户很多,每次都创建数据库的连接和销毁连接会有影响,所以创建数据库连接的时候我们可以使用连接池来做优化

没使用连接池的连接方法:

 使用了连接池的方法:

需要下载相应的第三方库才能让node驱动数据库:

1 npm install mysql2

准备数据-将json文件的数据插入到数据库中

从phpne.json文件里面获取json格式的数据并写到数据库里面。

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}

phone.jsond的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[
  {
    "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-基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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-预处理语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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-连接池使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)
})

到此这篇关于MySQL数据库node使用的文章就介绍到这了

分享到:

相关信息

  • MySQL存储函数以及触发器详解

    3). 删除 案例 A. 插入数据触发器 测试: B. 修改数据触发器 测试: C. 删除数据触发器...

    2023-10-27

  • mysql数据库连接失败常见问题小结

    错误提示:1045-Access denied for user ‘root‘ ‘localhost‘(using password: YES) mysql数据库只能本机连接,不能远程链接 宝塔没有放行端口,云服务器没有添加安全组...

    2023-10-27

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载