1-封装查询连接和根据条件查询数据是否存在
function querySql(sql) {
const conn = connect();
return new Promise((resolve, reject) => {
try {
conn.query(sql, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
})
} catch (e) {
reject(e);
} finally {
conn.end();
}
})
}
function queryOne(sql) {
return new Promise((resolve, reject) => {
querySql(sql).then(res => {
console.log('res===查询一条语句',res)
if (res && res.length > 0) {
resolve(res[0]);
} else {
resolve(null);
}
}).catch(err => {
reject(err);
})
})
}
2-获取表数据---sql语句
let query = `select d.id, d.username, d.department, d.year, d.month, d.status, d.time, d.userChinese from filltotal d`;
querySql(query).then((data) => {})
首先要明白d是对filltotal表的虚拟表,select 后面选择的元素是表里的字段,也就是说筛选出自己想要的字段。之后需要用封装的查询函数执行sql。
3-根据条件筛选数据---sql语句
let query = `select d.id, d.username, d.year, d.month, d.time from history d where username='${username}'`;
querySql(query).then((data) => {})
在查询数据后面加上 where,后面就是想要筛选的条件。
4-根据条件实现模糊查询---sql语句
let { department, year, month, status } = req.query;
//设置没有作为搜索时,将值设置为空字符串
department = department ? department : "";
year = year ? year : "";
month = month ? month : "";
status = status ? status : "";
let query_1 = `select d.id, d.username,d.userChinese, d.department, d.year, d.month, d.status, d.time from filltotal d where (department like '%${department}%') and (year like '%${year}%') and (month like '%${month}%') and (status like '%${status}%') `;
querySql(query_1).then((result_1) => {})
比如(department like '%${department}%')这里意思是 表中的字段department like 前端传过来的字段。达到模糊查询,需要注意的是,在前端传字段时需要进行判断,因为不存在时是undefined,需要转换为空字符串。
5-向数据库插入数据---sql语句
5-1使用字面量
data.forEach((item) => {
query = `insert into apply(username, year, month, project, percentage) values('${username}', '${year}', '${month}', '${item.project}', '${item.percentage}')`;
querySql(query).then(() => {})
})
这里由于data里有类似数组的解构,需要遍历插入多个数据。在遍历的内部,不能再写插入语句,这样会重复插入,只能放在循环外部。不是说只有字面量适合,只是在项目中用到了而已。
5-2使用占位符?
let addParams = [project, projectCode, operation];
let sql ="insert into manage(id,project,projectCode,operation) VALUES(0,?,?,?)";
conm.query(sql, addParams, (err, result) => {
if (err) {
res.json({
code: CODE_ERROR,
msg: "添加数据失败",
data: null,
});
} else {
res.json({
code: CODE_SUCCESS,
msg: "添加数据成功",
data: null,
});
}
});
addParams是前端传的字段,conm是连接数据库。
6-更新数据库部分字段---sql语句
updataHistory = `update history set time='${currentTime}' where username='${username}' and year='${year}' and month='${month}'`;
querySql(updataHistory).then(()=>{})
history是表名,time是需要更新的字段,where后面是所符合条件的数据。
7-封装根据条件查询数据是否存在
// 通过任务名称查询数据是否存在
function findTaskProject(username, year, month, type) {
let query = null;
if (type == 1) {
query = `select id, month, year, username from apply where username='${username}' and year='${year}' and month='${month}' `;
} else {
query = `select id, month, year, username from filltotal where username='${username}' and year='${year}' and month='${month}' `;
}
return queryOne(query);
}
//在逻辑中使用方式
findTaskProject(username, year, month, 1).then((task) => {
if(task){
//查询到之后需要做的事情
}
})
主要是用queryOne函数执行sql语句。
8-删除数据库符合条件的数据---sql语句
queryDelete = `delete from apply where username='${username}' and month='${month}' and year='${year}'`;
querySql(queryDelete).then(() => {})
根据指定的姓名、年、月,删除符合条件的信息