nodejs操作数据库 可以使用第三方模块 mysql
在vscode操作
1.安装 mysql 模块
cnpm i mysql -S
2.引入mysql模块
let mysql = require('mysql');
3.建立连接对象
let connection=mysql.createConnection({
host:"",
database:"",
user:'',
password:"",
port:3306;//可选参数
});
4.执行sql操作
connection.query(sql,(err,results,field)=>{
//err--操作mysql失败
//results--操作mysql成功
//field -- 查询数据每一个字段的解释
});
5.关闭连接
connection.end();
连接池 同时容纳多个连接对象 操作步骤
1.引入mysql
let mysql = require('mysql');
2.创建连接池对象
let pool=mysql.createPool({
host:"",
database:"",
user:'',
password:"",
port:3306;//可选参数,
//限制连接数量
connectionLimit:10
});
3.获取连接
pool.getConnection((err,connection)=>{
err--获取失败
connection--获取的连接对象
//操作sql
connection.query(sql,(err,results,filed)=>{
});
//释放连接对象
connection.release();
//关闭连接池
pool.end()
});
nodemon 自动构建部署服务
查询老师对应的课程 多表操作 单表操作
select * from tbl_teacher t,tbl_course c where c.teacher_id=t.id;
级联查询学生对应的选课信息
select * from tbl_sc sc,tbl_student s,tbl_course c where sc.student_id= s.id and sc.course_id=c.id;
7548

被折叠的 条评论
为什么被折叠?



