python 根据传进来的参数,动态拼接sql

根据传进来的参数,动态拼接sql,可用于实现一个方法,有些字段不确定,又用到的情况,如查询,三个查询条件,有的时候只用到一个查询条件,其他用不到则不需要拼接

 

 

 

    def show_runjob_detail(self, conditionList, valueList, fieldlist, rows):
        '''
        根据传进来的列名、具体的值和返回的字段的个数,动态查询定时任务的信息
        param:conditionList:要查询的具体的列名
        param:valueList:要查询具体列名的值
        param:fieldlist:select语句要查询出来的表的字段
        param:rows:行数
        '''
        fieldlist = []
        if len(fieldlist) == 0:
            fieldlist = ['id', 'job_id', 'test_suite_id', 'name', 'runtime', 'status']
        search_value = fieldlist[0]
        log.log().logger.info(fieldlist)
        for i in range(1, len(fieldlist)):
            # 将传进来的fieldlist拼接起来
            search_value = search_value + ','+fieldlist[i]
        condition = ' status not in (5) '
        for i in range(len(conditionList)):
            if len(valueList[i]):
                # 循环遍历列名和具体的值,有具体的value值,继续拼接sql
                condition = condition + ' and ' + str(conditionList[i]) + ' like "%' + str(valueList[i]) + '%"'
        sql = 'select ' + str(search_value) + ' from test_run_job where ' + str(condition) \
              + ' order by id desc limit '+str(rows)

  

posted on 2021-12-07 16:05  刚刚好1  阅读(2959)  评论(0编辑  收藏  举报

导航