当前位置:网站首页>Node的数据库编程(MySQL),增删改查
Node的数据库编程(MySQL),增删改查
2022-07-17 00:06:00 【别Null.了】
目录
在项目中操作MySQL数据库的步骤

1、安装操作MySQL数据库的第三方模块(mysql)
mysql 模块是托管于 npm 上的第三方模块。它提供了在 Node.js 项目中连接和操作 MySQL 数据库的能力。 想要在项目中使用它,需要先运行如下命令,将 mysql 安装为项目的依赖包: npm install mysql
2、通过mysql模块连接到MySQL数据库
配置mysql模块
//引入mysql2模块
const mysql = require('mysql2');
//2、创建数据库的连接
const connection = mysql.createConnection({
host:'localhost', //数据库服务器地址
port:3306, //mysql数据库的端口号
user:'root', //连接mysql的用户名
password:'******', //连接mysql的密码
database:'dbms' //要连接的数据库
})3、通过mysql模块执行SQL语句
执行数据库的操作
在我的数据库dbms中存在一张student表为:

查询数据
A、普通查询:query(sql,回调函数)
B、条件查询:query(sqlString,values,回调函数),参数values对应的是sqlString中的占位符
示例:查询student表中cno为3的学生信息(?表示占位符)
//查询数据库中的表格的数据,回调函数中err参数存放的是查询错误的信息,若查询成功则result参数中放的是查询的结果集
//条件查询
connection.query({
sql:'select * from student where cno = ?',
values:['3']
},(err,result)=>{
if(err){
console.log(err);
return;
}
console.log(result);
})
//普通查询
connection.query('select * from indent',(err,result)=>{
if(err){
console.log(err);
return;
}
console.log(result);
})条件查询的结果为:

插入数据
Insert into 表名(列名…) values(值…)
示例:向表student中插入一行数据
var addsql = "insert into stu(sno,cno,sname,sgender,sage,saddress) values(?,?,?,?,?,?)";
var addsql_params = ['12','3','刘备','男','29','陕西'];
connection.query({
sql:addsql,
values:addsql_params
},(err,result)=>{
if(err){
console.log(err);
return;
}
console.log('插入结果为:'+result);
})插入后的结果为:

修改数据
Update 表名 set 列名=值,… where 条件
注意:不加where条件就会把所有的数据都更新
示例:将刚才添加的数据中姓名修改为‘巴啦啦小魔仙’,年龄修改为27
var updatesql = "update student set sname=?,sage=? where sno=?";
var updatesql_params = ['巴啦啦小魔仙',27,'12'];
connection.query({
sql:updatesql,
values:updatesql_params
},(err,result)=>{
console.log("受影响的行数:"+result.affectedRows);
console.log("改变的行数:"+result.changedRows);
})
修改后的结果:

删除数据
Delete from 表名 [where 条件]
示例:删除刚才添加的sno为12的学生信息
var delsql = "delete from student where sno=?";
connection.query(delsql,'12',(err,result)=>{
if(err){
console.log(err);
return;
}
console.log("删除的行数:"+result.affectedRows);
})删除后的结果:

注意:防止sql的注入式攻击的方法:
注入式攻击:在进行语句的增删改查等操作时若有人拦截并添加一条永远满足的语句则会查询出所有的数据信息
select from stu where Sid=’S_1001’ (注入式攻击如在后面加or 1=1)
(1)使用占位符:?(提前预编译)
(2)使用connection.escape([参数字段])对值进行转义
边栏推荐
- 服务器如何安装宝塔面板?(宝塔面板安装教程)
- Colorful text advertising code, text advertising code beautification version, add text advertising tutorials to the website
- Day16 sorting, current limiting
- (三)传递参数
- Buuctf [bjdctf2020]ezphp1 detailed explanation
- Supplementary knowledge of attributes and methods of regular, JWT token, ronglianyun, celery, channel group, SKU, SPU, request object
- 使用bat自动执行cmd命令(多个命令或单个命令)
- XSS simple summary
- CTF CRYPTO RSA入门刷题
- C Programming Language (2nd Edition)--读书笔记--1.5.3
猜你喜欢

JSX compilation

网站被黑,通过百度/搜狗等搜索关键词访问跳转到其他网站怎么办?
![Eye of depth III - (6)] mathematics: matrix diagonalization and quadratic form 1](/img/6f/079f9d39b560a31fe3a0b57e0ff5aa.png)
Eye of depth III - (6)] mathematics: matrix diagonalization and quadratic form 1

Fundamentals of IOT development
![Record buuctf [netding Cup 2018] unfinish1 problem solving ideas](/img/29/6cf1eb89f1cbe087438645668ce103.png)
Record buuctf [netding Cup 2018] unfinish1 problem solving ideas
![Stack injection [strong net cup 2019] random note](/img/94/9069ed79e994e3c2e96c3cd1f816ee.png)
Stack injection [strong net cup 2019] random note

Registry hijacking triggers malicious programs

Uni app wechat applet - Mall (6) - my home page

当 std::bind 遇上 this

Collection and summary of penetration test information
随机推荐
Registry hijacking triggers malicious programs
extern和static的区别
jsx 编译
js替换字符串某个字符,js修改字符串中指定字符
C Programming Language (2nd Edition)--读书笔记--1.5.2
JS replaces a character in the string, and JS modifies the specified character in the string
three.js初体验:模拟一个小树杈的生长
If the website is hacked, what if you jump to other websites through Baidu / Sogou and other search keywords?
MoveIt2——5.场景规划
【ElenmentUI el-date-picker日期选择器,结束时间不得早于开始时间,且只能选择距开始时间指定天数的日期】
JSX 语法
JS string to object JS object to string JS string to object
Eye of depth III - (6)] mathematics: matrix diagonalization and quadratic form 1
The C Programing Language--(2nd)--笔记--4.11.2
everything搜索不到startup_lpc11x.s文件
JS intercepts the first few digits of the string or the last few digits of the string
2022年暑假ACM热身练习2(总结)
Oracle automatic storage management 18C step-by-step installation -1
C Programming Language(2nd Edition)--读书笔记--1.5
The C Programing Language-2nd--笔记--4.11.3