前端代码联调sql的增删改查


## 1.查询某个表的所有数据:注‼️联调数据库操作传参时值的类型'' ,  ""
//req封装PostgreSQL连接数据库(https://www.laike.net/article-28-44554-0.html)
let sqlParams = `select * from config_center_role where 1=1`;
      req(sqlParams).then(res => {
        this.tableData = res.rows;//数组
        this.total = res.rowCount;//总数
        this.paging();//前端分页方法见上一篇文章
      });

## 2.模糊查询某个表的数据
<div class="user-inp ">
        <el-form label-width="100px" class="demo-ruleForm" size="mini">
          <el-row>
            <el-col :span="12">
              <el-form-item label="类型:">
                <el-input v-model="model_type"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称:">
                <el-input v-model="model_name"></el-input>
              </el-form-item> </el-col></el-row
        ></el-form>
      </div>
//this.model_name 类型
//this.model_code 名称
      let sqlParams = `select * from cacu_center_model where 1=1 and model_name like CONCAT('%','${this.model_name}','%') and model_type like CONCAT('%','${this.model_code}','%')`;
req(sqlParams).then(res => {
        let tableData = res.rows;
        this.total = res.rowCount;
        this.paging();
      });


## 3.查询判断xx值是否xx表里存在
// this.loginForm.username前端查询的值

//写法1: select count(*)  》res.rows[0].count判断
let sql = `select count(*) from config_center_user where 1=1 and account ='${this.loginForm.username}'`;
      console.log(sql);
      req(sql).then(res => {
        if (res.rows[0].count== 0) {
          this.$message.error('账号不存在!');
        } else {
        	.....
        }
})



//写法2: select *   》res.rowCount判断
let sql = `select * from config_center_user where 1=1 and account ='${this.loginForm.username}'`;
      console.log(sql);
      req(sql).then(res => {
        if (res.rowCount== 0) {
          this.$message.error('账号不存在!');
        } else {
        	.....
        }
})

增:

//雪花算法:(https://blog.csdn.net/qq_45634989/article/details/127389119)
import SnowflakeId from 'snowflake-id';

addData() {
      const snowflake = new SnowflakeId({
        mid: 42,
        offset: (2019 - 1970) * 31536000 * 1000
      });
      var id = snowflake.generate();
      let time = format(this.formList.model_start_up, 'yyyy-MM-dd hh:mm:ss');
      let sqlParams = `('${id}', '${this.userId}','${this.formList.model_name}','${this.formList.model_range}', '${this.formList.model_type}','${time}','${time}', '${this.formList.model_cycle}', '${this.formList.cycle_unit}','${this.modelId}','${this.paramsId}','${this.model_status}','${this.formList.model_code}','${this.funCode}')`;
      let sql = `INSERT INTO cacu_center_model (id, uid, model_name, model_range, model_type, model_start_up,next_start_time, model_cycle, cycle_unit,model_file,param_file,model_status,model_code,model_function) VALUES ${sqlParams}`;
      console.log(sql);
      req(sql)
        .then(res => {
          if (res.rowCount !== 0) {
            this.$message.success('新增成功!');
          } else {
            this.$message.error('新增失败!');
          }
          this.getData();
        })
        .catch(err => {});
    },

删:

//表名
 let sql = `delete from cacu_center_model where id = '${this.row_Id}'`;
      req(sql).then(res => {
        if (res.rowCount !== 0) {
          this.$message.success('删除成功!');
        } else {
          this.$message.error('删除失败!');
        }
      });

改:

//修改表格信息
      let time = format(this.formList.model_start_up, 'yyyy-MM-dd hh:mm:ss');
      let sql = `update cacu_center_model set model_name = '${this.formList.model_name}',model_range = '${this.formList.model_range}',model_type = '${this.formList.model_type}',
      model_start_up = '${time}',next_start_time = '${time}',model_cycle ='${this.formList.model_cycle}', cycle_unit ='${this.formList.cycle_unit}', model_file = '${this.model_file}', param_file ='${this.param_file}',model_status='${this.model_status}',model_function='${this.funCode}' where id = '${this.rowId}'`;
      req(sql).then(res => {
        this.$message.success('编辑成功!');
      });```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值