Node.js系列之连接DB的方法(3)
作者:Sherry&Yang 时间:2024-05-03 15:55:26
背景
node.js,有人称之为运行在服务器端的JavaScript。以往我们使用JavaScript时,都是依赖后端查询数据库并返回数据,而JavaScript只需要展示即可。问题来了,就不能绕开后端直接查询数据库吗?答案是肯定的。
关系型——以mysql操作为例
node.js提供了mysql操作的驱动(模块),如果还未安装,需要执行 npm install mysql 命令来安装。
//引入模块
var mysql=require('mysql');
//定义连接对象connection
var connection=mysql.createConnection({
host:'192.168.*.*',
user:'root',
password:'******',
database:'DB'
});
//连接
connection.connect();
//执行查询测试
var sql='select * from sys_user';
connection.query(sql,function(err,result){
if(err){
console.log('[select error]-',err.message);
return;
}
console.log('-----select-------');
console.log(result);
console.log('------------------------');
});
//关闭连接
connection.end();
部分查询结果如下:
node.js连接mysql的其他增删改操作就不一一展示了,和查询操作类似,准备好sql语句,使用提供的响应的方法就ok了。
非关系型——以mongoDB为例
安装驱动模块
npm install mongodb
插入操作
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://192.168.*.*:27017/runoob';
var insertData = function(db, callback) {
// 连接到表 site
var collection = db.collection('site');
// 准备数据
var data = [{"name":"Sherry","sex":"female","email":"xxxxx@163.com"},{"name":"Tom","sex":"male","email":"xxxx@163.com"}];
collection.insert(data, function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
callback(result);
});
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
console.log("连接成功!");
insertData(db, function(result) {
console.log(result);
db.close();
});
});
执行代码:
可以看到插入日志
使用命令mongodb/bin/mongo进入mongo客户端,可以看到刚刚插入的数据。
总结
从操作来看,node.js操作数据库还是比较简单的,底层实现依赖封装的mysql/mongodb模块。
从场景来看,node.js虽然可以实现大多数应用的功能,但并非要替代其他技术或产品,而是针对不同情况做出适合的选择。
来源:https://www.cnblogs.com/saixing/p/6816844.html
标签:Node.js,DB
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python学习之str重要函数
2022-12-29 05:46:37
数据库性能优化二:数据库表优化提升性能
2024-01-22 12:07:19
![](https://img.aspxhome.com/file/2023/0/88270_0s.png)
golang中new与make的区别讲解
2024-05-09 14:55:15
![](https://img.aspxhome.com/file/2023/8/127378_0s.png)
vue-cli与webpack处理静态资源的方法及webpack打包的坑
2024-05-09 09:39:07
PyTorch与PyTorch Geometric的安装过程
2021-12-15 05:00:08
![](https://img.aspxhome.com/file/2023/0/83830_0s.png)
详解Python 函数如何重载?
2021-04-11 04:48:30
如何做一个文本搜索?
2010-07-12 19:00:00
python Pandas中数据的合并与分组聚合
2023-06-28 12:48:04
![](https://img.aspxhome.com/file/2023/9/118279_0s.png)
MySQL数据库的主从同步配置与读写分离
2024-01-20 17:50:58
python读写ini配置文件方法实例分析
2022-10-09 22:02:28
DreamweaverMX制作导航下拉菜单
2010-07-02 16:29:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113323939s.jpg)
Python 处理表格进行成绩排序的操作代码
2022-08-25 02:49:36
![](https://img.aspxhome.com/file/2023/3/104913_0s.jpg)
Python实现判断一个字符串是否包含子串的方法总结
2023-04-17 06:27:58
Python爬取网站图片并保存的实现示例
2023-06-05 18:01:29
![](https://img.aspxhome.com/file/2023/3/89373_0s.jpg)
使用DW中遇到的常见问题详解
2008-03-18 16:27:00
对matplotlib改变colorbar位置和方向的方法详解
2023-07-13 21:00:04
css学习笔记:div在IE6下无法遮盖select
2009-04-30 13:21:00
![](https://img.aspxhome.com/file/UploadPic/20094/30/11f2ae205c6g215-66s.jpg)
PyTorch的深度学习入门教程之构建神经网络
2021-11-21 09:34:22
python实现计数排序与桶排序实例代码
2023-12-28 11:32:37
![](https://img.aspxhome.com/file/2023/9/82899_0s.png)
Python爬虫解析网页的4种方式实例及原理解析
2022-11-03 03:11:23
![](https://img.aspxhome.com/file/2023/9/118739_0s.jpg)