当前位置:网站首页>nodejs项目连接mysql数据库
nodejs项目连接mysql数据库
2022-08-10 23:48:00 【不能懒鸭】
npm i mysql
const mysql = require('mysql')
const db = mysql.createPool({
host:'127.0.0.1',//数据库的IP,此处为自己电脑上的mysql
user:'root',//账号
password:'root',//密码
database:'my-db_01'//名称
})
//测mysql模块是否正常工作
db.query('select 1',(err,results)=>{
if(err) return console.log('数据库连接失败',err.message)
console.log('数据库连接成功',results)
})
//测试:执行一条查询语句
const sql1 = 'SELECT * FROM users'
db.query(sql1,(err,results)=>{
if(err) return console.log('数据库连接失败',err.message)
console.log(results) //数组
})
const user = {
username:'王大大',
password:'009'
}
//测试:执行插入语句
const sql2 = 'INSERT INTO users (username,password) values(?,?)' // ?为占位符
db.query(sql2,[user.username,user.password], (err,results)=>{
if(err) return console.log(err.message)
if(results.affectedRows==1){
console.log('插入成功')
}
})
- 启动node项目,运行结果如下:
边栏推荐
- In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?
- Web-based meal ordering system in epidemic quarantine area
- 如何快速把握行业机会,更高效地推陈出新,是一个重要的命题
- 从0开始设计JVM ,忘记名词跟上思路一次搞懂
- [数据可视化] 图表设计原则
- “蔚来杯“2022牛客暑期多校训练营4 ADHK题解
- 【C语言】C语言程序设计:动态通讯录(顺序表实现)
- 部分准备金银行已经过时
- Geogebra 教程之 02 Geogebra初学者的 8 个基本要素
- Dump file generation, content, and analysis
猜你喜欢
11. 自定义转换器
String
10. 接收参数相关注解
鲜花线上销售管理系统的设计与实现
Multilingual Translation - Multilingual Translation Software Free
工程师如何对待开源
excel英文自动翻译成中文教程
[Excel knowledge and skills] Convert text numbers to numeric format
How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
Dump file generation, content, and analysis
随机推荐
一条SQL查询语句是如何执行的?
Ali P7 bask in January payroll: hard to fill the, really sweet...
闭包详解,柯里化的含义及操作方法
如何快速把握行业机会,更高效地推陈出新,是一个重要的命题
iNFTnews | In the Web3 era, users will have data autonomy
5. Lombok
【C语言】二分查找(折半查找)
Cache knowledge summary
13. Content Negotiation
14. Thymeleaf
图像识别和语义分割的区别
后疫情时代,VR全景营销这样玩更加有趣!
promise详解
【C语言篇】操作符之 位运算符详解(“ << ”,“ >> ”,“ & ”,“ | ”,“ ^ ”,“ ~ ”)
开启新征程——枫叶先生第一篇博客
In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?
UOJ#749-[UNR #6]稳健型选手【贪心,分治,主席树】
16. 文件上传
iNFTnews | Web3时代,用户将拥有数据自主权
服务器小常识