简体   繁体   中英

Concatenate SQL string with args

I am trying to use a default query and then append some conditions. I would like to know how can i concatenate a string while unpacking args.

filters = []
if industry:
    filters.append('industry_id')
    filters.append('role_id')
    ...

As far as I can see this doesn't work (invalid syntax).

print "SELECT city, " + *filters + " FROM histogram"

Use join to do what you need:

print "SELECT city, " + ', '.join(filters) + " FROM histogram"

which prints:

SELECT city, industry_id, role_id FROM histogram

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM