Python - Generate a multiple row update query <p>My goal is dynamically generate in Python a SQL query similar to,</p> <pre><code>UPDATE SURV_SCF_ID_STATUS SET AGE_STATUS = CASE NAME WHEN 'entityXp1' THEN '1' WHEN 'entityXp3' THEN '0' WHEN 'entityXpto1' THEN '1' WHEN 'entityXpto3' THEN '1' END WHERE NAME IN ('entityXp1', 'entityXp3', 'entityXpto1', 'entityXpto3') </code></pre> <p>This is what I have so far, but still feels like an ugly hack,</p> <pre><code>logs_age_list = [['entityXp1', '1'], ['entityXp3', '0'], ['entityXp1', '1'], ['entityXpto3', '1']] conditions_list = [] where_list = [] for entity, status in logs_age_list: conditions_list.append("\t\tWHEN %(entity)s THEN %(value)s" % locals() ) where_list.append(entity) conditions_string = '\n'.join(conditions_list) where_string = ', '.join(where_list) sql = ''' UPDATE SURV_SCF_ID_STATUS SET AGE_STATUS = CASE NAME %(conditions_string)s END WHERE NAME IN (%(where_string)s) ''' % locals() print sql </code></pre> <p>Is there some more obvious way to do it? I didn't find any Python module which allow this.</p> <p>Thanks</p>