python-sqlalchemy-原生sql

                增删改查条件分页-仅接口测试

控制器代码

@config_blue.route('/config/save/', methods=['POST'])
def save_config():
    # authorname=request.form.get('authorname')
    if request.method == 'POST':
        post_data = request.get_json()
        # return jsonify({"code": 201, "msg": len(post_data.get('apn'))})
        if len(post_data.get('apn')) == 0:
            sql = "UPDATE `pythonConfig` SET `plmn`='',`tac`='',`sd`='',`sst`='',`apn`='',`ue_ip_end`='',`ue_ip_start`='' WHERE `id`='1'"  
            return Response.success() if pythonConfigDas.execute(sql) else Response.error()
        elif len(post_data.get('apn')) > 0: 
            plmn = repr(post_data.get('plmn')).strip()
            tac = repr(post_data.get('tac')).strip()
            sd = repr(post_data.get('sd')).strip()
            sst = repr(post_data.get('sst')).strip()
            apn =   '"'+repr(post_data.get('apn')).strip()+'"'
            ue_ip_end = repr(post_data.get('ue_ip_end')).strip()
            ue_ip_start = repr(post_data.get('ue_ip_start')).strip()
            sql = "UPDATE `pythonConfig` SET `plmn`=%s,`tac`=%s,`sd`=%s,`sst`=%s,`apn`=%s,`ue_ip_end`=%s,`ue_ip_start`=%s WHERE `id`='1'" %(plmn,tac,sd,sst,apn,ue_ip_end,ue_ip_start)
            # return jsonify({"code": 201, "msg": sql})

            sql = sql.replace('\\', " ")
            return Response.success() if pythonConfigDas.execute(sql) else Response.error()
        else:
            return jsonify({"code": 201, "msg": "操作失败"})
 
##
@config_blue.route('/config/rightList/', methods=['POST'])
def rightList_config():
    if request.method == 'POST':
        post_data = request.get_json()
        tac = repr(post_data.get('tac')).strip()
        page = post_data.get('page')    ##当前页码
        pageSize = post_data.get('pageSize')  ##每页条数
        sql = "select plmn,tac,sd,sst,apn,ue_ip_end,ue_ip_start FROM pythonConfig  where   `tac` =%s  limit %d,%d " %(tac,(page-1)*pageSize,pageSize)
        data_config = pythonConfigDas.select(sql)  ##数据
        
        ##符合条件的总个数
        count_sql = " select count(*) as count FROM pythonConfig  where `tac` =%s" %(tac)
        total = pythonConfigDas.selectCount(count_sql)[0]['count'] ##数据总条数
        pagetotal = 0 ##页码总数
        if total % pageSize == 0:
            pagetotal = total/pageSize
        else:
            pagetotal = total/pageSize+1
        ##返回数据
        return jsonify({
                "code": 200,
                "msg": "",
                "data":{
                    "page":page, ##当前页码
                    "total":total,##数据总数
                    "pagetotal":pagetotal,##页码总数
                    "list":data_config
                }   
        })

表模型


from core_network import db
from core_network.tools.warps import das_warps


class pythonConfigDas:

    ##增删改
    @staticmethod
    @das_warps
    def execute(sql):
        db.session.execute(sql)
        db.session.commit()
        return True
   
    ##查询多条数据
    @staticmethod
    def select(sql):
        data = db.session.execute(sql).fetchall()
        ##结果集必须是字典的形式,不要直接返回!!!
        result = [dict(zip(result.keys(), result)) for result in data]
        return result
    ##查询个数
    @staticmethod
    def selectCount(sql):
        count = db.session.execute(sql)
        result = [dict(zip(result.keys(), result)) for result in count]
        return result

无数据

有数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值