当前位置:网站首页>nodejs中使用mySql数据库
nodejs中使用mySql数据库
2022-06-11 00:14:00 【呵呵的牛】
nodejs中使用mySql数据库
nodejs 中怎么去链接mysql数据库?
本地安装mySql数据库 网上安装教程很多可以自行百度。
1.链接本地数据库

2.本地创建test.js文件
var mysql = require('mysql');
var connection = mysql.createConnection({
//本地的话就是localhost
host:'localhost',
//mysql的用户名
user:'root',
//mysql的密码
password:'123456',
//数据库名
database:'test'
})
connection.connect();
websites为test数据库中的表
//查询所有 websites 表中的数据
var websit = 'SELECT * FROM websites WHERE name="菜鸟工具"'
//查询数据库
connection.query(websit,function(error,results,fileds){
if(error) {throw error};
console.log('The solution is:',results,'查询数据成功');
})
这是websites中的数据

终端执行 node test.js输入websites表中所有的数据

可以返回mySql中websites中的所有数据。说明链接mySql数据库成功
3.新增数据
//增数据
var addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
var addSqlParams = ['菜鸟工具','https://c.runoob.com','2222','CN'];
connection.query(addSql,addSqlParams,function(err,results){
if(err){
console.log(err.message);
return
}
console.log(results,'新增数据成功')
})

执行node test.js 会显示新增数据成功

数据库中就会多了一条 新增的数据
4.更新数据
//修改数据
var modSql = 'UPDATE websites SET name = ? ,url = ? WHERE Id = ?';
var modSqlParams = ['菜鸟驿站000','www.cainiaoyizhan.com',17];
connection.query(modSql,modSqlParams,function(err,results){
if(err){
console.log(err.message)
return
}
console.log(results,'修改成功')
})
执行node test.js 返回下面内容

此时的id为17的数据就修改成了 菜鸟驿站000了

5.删除数据
//删除数据
var delSql = 'DELETE FROM websites where id = 17';
connection.query(delSql,function(err,results){
if(err){
console.log(err.message)
return
}
console.log(results,'删除id为6的数据');
})
执行node test.js 会删除掉id为17的这条数据

表中的id为17的数据 就被删除了
边栏推荐
- Slam Kalman filter & nonlinear optimization
- 快递鸟系统对接
- 最好的创意鼓工具:Groove Agent 5
- 【NVIDIA驱动的顽固问题】---- /dev/sdax:clean,xxx/xxx files,xxx/xxx blocks ---- 最全解决方法
- Pd虚拟机安装系统提示 “网络初始化失败 操作失败 ”的解决方案
- ion_dma_buf_begin_cpu_access
- Some idle gossip
- [introduction to ROS] - 03 basic concepts and instructions of ROS
- Array simulation [queue] and [ring queue]_ code implementation
- 时间相关-格式、运算、比较、转换
猜你喜欢
[ROS tutorial] - 02 ROS installation

Summary of pytorch classification problems

中小企业数字化转型为什么这么难?

明朝的那些皇帝
![[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism](/img/ad/d798284ceb370f7c68cbab755a11de.png)
[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism

Why web development with golang

Why is the digital transformation of small and medium-sized enterprises so difficult?
![[论文阅读] BoostMIS: Boosting Medical Image Semi-supervised Learning with Adaptive Pseudo Labeling](/img/10/a60cfe830e2238de00121d7bd95ad6.png)
[论文阅读] BoostMIS: Boosting Medical Image Semi-supervised Learning with Adaptive Pseudo Labeling

Centos7 actual deployment mysql8 (binary mode)

最好的創意鼓工具:Groove Agent 5
随机推荐
MySql 触发器
Logback log framework
Array simulation [queue] and [ring queue]_ code implementation
Centos7 actual deployment mysql8 (binary mode)
【原】expdp参数CONTENT
Small project on log traffic monitoring and early warning | environment and foundation 1
程序员应对压力的几个小窍门
Locks in sqlserver
Win11 uninstall widget
Serrures dans SQLSERVER
【ROS入门教程】---- 03 ROS工作空间与功能包
Introduction and creation of Huffman tree
Redis data has been overwritten
一些有的没的闲话
【NVIDIA驱动的顽固问题】---- /dev/sdax:clean,xxx/xxx files,xxx/xxx blocks ---- 最全解决方法
SqlServer中的锁
ion_dma_buf_begin_cpu_access
Deep copy and shallow copy in golang
WIN11卸载小组件
SQL audit | "cloud" users can use the SQL audit service with one click
