当前位置:网站首页>Node的数据库编程
Node的数据库编程
2022-04-23 05:53:00 【遥岑.】
Node的数据库编程
Node的MySQL驱动:mysql2是node的原生mysql驱动的升级版。
安装库:npm install mysql2。
建立连接
显式连接:
- 导入
mysql2模块
//引入模块
const mysql = require('mysql2')
- 调用
createConnection方法创建数据库的连接对象
//创建数据库连接对象
const connection = mysql.createConnection({
host:'localhost',
port:3306,
user:'root',
password:'111111',
database:'project'
})
- 调用
connect方法连接数据库
//调用connect方法连接数据库
connection.connect((err)=> {
if(err) {
throw err
}
console.log(connection.threadId)
})
- 调用
query方法执行sql查询
//查询数据
connection.query('select * from employee',(err,result)=> {
if(err) {
console.log(err)
return
}
console.log(result)
})
隐式连接:
- 导入mysql2模块
- 调用createConnection方法创建数据库的连接对象
- 调用query方法执行sql查询(query已经隐式连接上数据库了)
终止与MySQL的连接
- 调用
end方法
connection.end()
- 调用
destroy方法
connection.destroy()
执行数据库的操作(CRUD)
调用query方法。
查询记录:
- 简单查询:query(sql,callback)
connection.query('select * from employee',(err,result)=> {
if(err) {
console.log(err)
return
}
console.log(result)
})
- 条件查询:query(sqlString, values, callback)
参数values对应的是sqlString中的占位符?
var sqlString = 'select * from employee where address = ?'
connection.query(sqlString,['金陵'],(err,result)=> {
if(err) {
console.log(err)
return
}
console.log(result)
})
- 参数是对象:query(options, callback)
connection.query({
sql:'select * from employee where address=?',
values:['金陵']
},(err,result)=> {
if(err) {
console.log(err)
return
}
console.log(result)
})
增加记录:
insert into 表名(列名…) values(值…)
var add = 'insert into employee(name,gender,birthday,phone,address) values(?,?,?,?,?)'
var addSql = ['邓瑛','男','1990-10-31','11111111','大明']
connection.query({
sql:add,
values:addSql
},(err,result)=> {
if(err) {
console.log(err)
return
}
console.log(result.insertId)
console.log(result)
})
更新记录:
update 表名 set 列名=值,… [where 条件]
var updatSql = 'update employee set address = ? where name = ?'
var update = ['长安','邓瑛']
connection.query({
sql:updatSql,
values:update
},(err,data)=> {
if(err) {
console.log(err)
return
}
console.log(data.affectedRows)
console.log(data.changedRows)
})
删除记录:
delete from 表名 [where 条件]
var delSql = 'delete from employee where name=?'
connection.query({
sql:delSql,
values:'邓瑛'
},(err,result)=> {
if(err) {
console.log(err)
return
}
console.log(result.affectedRows)
})
防止注入式攻击
使用占位符?
使用connection.escape([参数字段])对值进行转义
数据库的连接池技术
数据库连接池是程序启动时建立足够数量的数据库连接对象,并将这些连接对象组成一个池, 由程序动态地对池中的连接对象进行申请、使用和释放。
数据库的连接池负责分配、管理和释放数据库连接对象的。它允许应用程序重复使用一个现有的数据库的连接对象,而不是重新创建一个。
这样可以避免应用程序频繁的连接、断开数据库,提高数据库连接对象的使用效率。
数据库连接池的使用
创建数据库连接池:mysql.createPool(config)
//导入mysql模块
const mysql = require('mysql2');
const pool = mysql.createPool({
connectionLimit:20,
host:'localhost',
port:3306,
user:'root',
password:'200173',
database:'project',
multipleStatements:true
})
- host:数据库服务器的地址
- port: 端口号
- user:连接数据库的用户名
- password:连接数据库的密码
- database:数据库名
- connectionLimit:用于指定连接池中最大的链接数,默认属性值为10.
- multipleStatements :是否允许执行多条sql语句,默认值为false
从连接池中获取一个连接:
连接池名.getConnection(function(err,connection){
执行的代码
})
参数err:错误对象。连接失败后的错误信息
参数connection:连接对象。若连接失败,它就是undefined
pool.getConnection((err, connection) => {
if (err) {
throw err;
}
//利用数据库连接对象执行查询
connection.query('select * from employee', (e, result) => {
if (e) {
throw e;
}
res.write(JSON.stringify(result)); //将数据库的查询结果转换成JSON格式响应给前端
res.end();
})
connection.release(); //释放数据库连接对象
})
释放连接对象(将连接对象放回连接池):
connection.release();
从连接池中移除连接对象:
connection.destory();
关闭该连接池:
连接池名.end();
数据库访问中的ORM
ORM:对象关系映射,主要解决面向对象编程与关系型数据库之间不匹配的问题。
- 类 ---- 表
- 属性 ---- 列
- 对象 ---- 行
它可以提高开发的效率,不用再直接写sql语句。
使用
ORM的实现框架(模块):sequelize。
安装:
npm install sequelize
连接数据库:创建sequelize的对象
//导入sequelize模块
const Sequelize = require('sequelize');
//创建sequelize对象
var MySequelize = new Sequelize('dbms','root','123456',{
host: 'localhost',
port: 3306,
dialect: 'mysql', //数据库类型
pool: {
//数据库连接池
max: 20, //最大连接对象的个数
min: 5, //最少连接对象的个数
idle: 10000 //最长等待时间,单位是毫秒
}
});
创建数据模型:数据模型是一个类,对应的是数据库中一张表
- 使用
define方法创建 - 使用Sequelize.Model.init(attributes, options)函数
const Sequelize = require('sequelize');
const MySequelize = require('../config/dbconfig');
//创建StudentModel模型,该模型对的表名是student
var StudentModel = MySequelize.define('student',{
sid: {
type: Sequelize.INTEGER, //表示属性的数据类型
field: 's_id', //属性对应的列名,若不定义field,则表中的列名就是属性名
primaryKey: true, //表示主键
autoIncrement: true //表示主键自增
},
sname: {
type: Sequelize.STRING(50),
field: 's_name',
allowNull: false, //表示该列不能为空
//unique: true //表示该列的值必须唯一
},
sgender: {
type: Sequelize.STRING(4),
field: 's_gender',
allowNull: false
},
sbirthday: {
type: Sequelize.DATE,
field: 's_birthday',
allowNull: false
},
saddress: {
type: Sequelize.STRING(100),
field: 's_address',
allowNull: false
}
},
{
freezeTableName: true, //true表示使用给定表名,false表示模型名加s作为表名
timestamps: false //true表示给模型带上时间戳属性(createAt、updateAt),false表示不带时间戳属性
}
);
// var student = StudentModel.sync({force: false}); //同步数据库,force的值为false,表若存在先删除后创建,force的值为true,表示表如果存在则不创建
module.exports = StudentModel;
进行CRUD操作:
插入数据:
模型名.create({
属性名1:值1,
属性名2:值2,
......
}).then((result)=> {
插入成功后代码; 参数'result'中放的是插入成功的数据
}).catch((error)=> {
插入失败后的代码;参数'error'中放的是插入失败的信息
})
删除数据:
模型名.destroy({
where:{
属性名:值
}
}).then((result)=> {
删除成功后代码; 参数'result'中放的是删除的行数(整数)
}).catch((error)=> {
删除失败的处理代码,参数'error'中放的是删除失败的信息
})
更新数据:
模型名.findOne({
where: {
属性名:值
}
}).then((result)=> {
//参数'result'中放的是查找到的数据
result.update({
属性名1:值1,
属性名2:值2
}).then((data)=> {
//参数'data'中放的是更新后的数据
处理代码
}).catch((error)=> {
处理代码
})
}).catch((err)=> {
未查到记录的处理代码
})
查询数据:
模型名.findAll({
where:{
属性名:值
}
}).then((result)=> {
参数'result'中放的是查询的结果集
}).catch((error)=> {
参数'error'中放的是查询失败的信息
})
版权声明
本文为[遥岑.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_52942098/article/details/120982968
边栏推荐
- Matching between class template with default template argument and template parameter
- Makefile foundation, common functions and general makefile
- Using printf in MFC
- 友元函数,友元类,类模板
- 赛氪-zeal
- Shell脚本 单引号、双引号和反引号的区别
- [UDS unified diagnosis service] i. diagnosis overview (2) - main diagnosis protocols (K-line and can)
- 算数表达式
- 生成快捷方式
- ES6面试题(参考文档)
猜你喜欢
VHDL-任意分频器(50%占空比)

QT add qserialport class to realize serial port operation
![[UDS unified diagnosis service] i. diagnosis overview (2) - main diagnosis protocols (K-line and can)](/img/ec/9b783b6dca5684684485e3b9fc6846.png)
[UDS unified diagnosis service] i. diagnosis overview (2) - main diagnosis protocols (K-line and can)

Analysis and setting of dead time

Informatics one book pass - small ball

Makefile foundation, common functions and general makefile

CUDA environment installation
![[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)](/img/4f/315a9b4cd85ebaad39cfa985dea45b.png)
[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)
![[UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture](/img/ef/173281ffb354b9abe1b730b89469cc.png)
[UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture

记第一次使用阿里字体图标库
随机推荐
JS中的this指向
日志写法(带时间)
The use of volatile in C language
[UDS unified diagnostic service] III. application layer protocol (1)
File viewing commands and user management commands
Understanding of SSH public key and private key
Opencv uses genericindex for KNN search
生成快捷方式
HDU-Memory Control
信息学一本通-小球
提交本地仓库并同步码云仓库
Incremental update of client software
时间戳转格式化日期
锚点定位——如何设置锚点居页面顶部距离,锚点定位并距离顶部一定偏移
Error in created hook: “ReferenceError: “Promise”未定义“
POJ-The Unique MST
查漏补缺(六)
修改注册表的值
[learn] HF net training
微信小程序之 js 时间戳/1000 转换 秒,六个小时后,一天后,本周五 选项计算时间