时间: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使用的文章就介绍到这了
2023-10-30
windows上的mysql服务突然消失提示10061 Unkonwn error问题及解决方案2023-10-30
MySQL非常重要的日志bin log详解2023-10-30
详解MySQL事务日志redo log一、单表查询 1、排序 2、聚合函数 3、分组 4、limit 二、SQL约束 1、主键约束 2、非空约束 3、唯一约束 4、外键约束 5、默认值 三、多表查询 1、内连接 1)隐式内连接: 2)显式内连接: 2、外连接 1)左外连接 2)右外连接 四...
2023-10-30
Mysql删除表重复数据 表里存在唯一主键 没有主键时删除重复数据 Mysql删除表中重复数据并保留一条 准备一张表 用的是mysql8 大家自行更改 创建表并添加四条相同的数据...
2023-10-30