有许多if语句的For循环

时间:2019-06-14 17:30:40

标签: python

我正在尝试“即时”生成SQL语句。

编辑:我还需要进行以下操作:

输出看起来很像

sql_cols => DESCRIPTION,  FIRSTNAME,  LASTNAME
sql_vals => :1,:2,:3

OR

sql_cols => FIRSTNAME,  LASTNAME
sql_vals => :1,:2

或下面的任何其他

sql_cols => FIRSTNAME
sql_vals => :1

sql_cols => DESCRIPTION,  LASTNAME
sql_vals => :1,:2

sql_cols => LASTNAME
sql_vals => :1

我几乎在一起。唯一的事情是必须重复一些代码。特别是“ if”语句”

    if ( row == 'firstname' ) :
        firstname = item['firstname']
        sql_cols  += ' FIRSTNAME, '
        sql_vals  += ':' + str(count) + ',' 
        sql_inputs.append( firstname ) 

    if ( row == 'lastname' ) :
        lastname = item['lastname']
        sql_cols  += ' LASTNAME, '
        sql_vals  += ':' + str(count) + ',' 
        sql_inputs.append(  lastname ) 

    if ( row == 'description' ) :
        description = item['description']
        sql_cols  += ' DESCRIPTION, '
        sql_vals  += ':' + str(count) + ',' 
        sql_inputs.append( description ) 

一个人该如何重写它以便不使用那么多的“ if”语句?

TIA

输入

req_data = {
"list":[
    { "description" : "tall", "firstname":"very high", "lastname":"very heavy" },
    { "description" : "short", "firstname":"no high", "lastname":"very light" },
    { "firstname" : "heay" },
    { "lastname" : "light" }
  ]
}

更改代码

def test_create_data():
     try:
        data = []
        for item in req_data['list']:
            sql_cols  = ''
            sql_vals= ''
            sql_inputs = [] 
            sql = 'INSERT INTO newtest '
            count = 1
            for row in item:

                if ( row == 'firstname' ) :
                    firstname = item['firstname']
                    sql_cols  += ' FIRSTNAME, '
                    sql_vals  += ':' + str(count) + ',' 
                    sql_inputs.append( firstname ) 

                if ( row == 'lastname' ) :
                    lastname = item['lastname']
                    sql_cols  += ' LASTNAME, '
                    sql_vals  += ':' + str(count) + ',' 
                    sql_inputs.append(  lastname ) 

                if ( row == 'description' ) :
                    description = item['description']
                    sql_cols  += ' DESCRIPTION, '
                    sql_vals  += ':' + str(count) + ',' 
                    sql_inputs.append( description ) 

                count = count + 1 

            #get rid of the last "," that is on each of the strings
            result = sql_cols.rsplit(',', 1)[0]
            sql_cols = result

            result = sql_vals.rsplit(',', 1)[0]
            sql_vals = result

2 个答案:

答案 0 :(得分:5)

基于row的值唯一改变的是附加到sql_cols的字符串,而只是row转换为大写。追加到item['description']后,您可以直接使用sql_inputs

sql_vals = []
sql_cols = []
sql_inputs = []
for count, row in enumerate(item, start=1):
    if row not in {'firstname', 'lastname', 'description'}:
        continue

    sql_inputs.append(item[row])
    sql_vals.append(count)
    sql_cols.append(row)

sql_cols = " ".join(x.upper() for x in sql_cols)
sql_vals = ",".join(":{}".format(x) for x in sql_vals)

答案 1 :(得分:4)

如果您每次都做相同的事情,则无需检查行名。

for row in item:
    sql_cols  += f' {row.upper()}, '
    sql_vals  += ':' + str(count) + ','
    sql_inputs.append(item[row])