在Nodejs中mysql增删改查及链表
1.连接mysql数据库
let mysql=require('mysql')
let connextion=mysql.createConnection({
host:'localhost',
// post:'3306',//可选,默认3306
user:'root',//用户没
password:'root',//密码
database:'testdemo',//数据库名称
})
connextion.connect((err)=>{//建立连接
if(err){
console.log(err)
}else{
console.log('mysql连接成功')
}
});
2.增删改查,模糊查询,链表
因为这里我们没有使用任何框架,所以使用的mysql原生语句
// let strSql="drop table user" //删除表
// let strSql="drop database testdemo" //删除库
// let strSql="create database testdemo" //创建库
// let strSql="select * from user" //查询数据
// let strSql="select * from user where id=2" //查询数据
// let strSql="select * from user where id>=2" //查询数据
// let strSql="select * from user where id>=2 and uaername='嘿嘿'" //查询数据 and是且---or是或
// let strSql="select * from user where uaername like '%小%'" //模糊查询
// let strSql="select * from user where uaername is NuLL" //查询真正的空,不是空字符串
// let strSql="select * from user where uaername is not NuLL" //查询不是空
var delSql = 'DELETE FROM user where id=6';//删除
var updSql = 'UPDATE user SET name = ?,url = ? WHERE Id =2';//更新
// let strSql="select * from user inner join teacher on user.classname=teacher.class" //连表查询
// let strSql="select * from user inner join teacher on user.classname=teacher.class where user.id=2" //连表查询
let strSql="select * from user right join teacher on user.classname=teacher.class where teacher.teacher_name='王老师'" //连表查询
// let strSql="insert into user (id,uaername,password) values (3,'嘿嘿','654564')" //插入数据
connextion.query(strSql,(err,res,fields)=>{
console.log(res)
})