Mostly Linux & Python syntax notes and hyperlinks.

Monday, October 25, 2010

Python: Convert Python List to SQL "IN" List

    py_list=[123,2343,433,454]

    #convert the python list into a string to be used in SQL "IN" query
    sql_list=""
    quote = '\''
    for item in py_list:
        sql_list+= quote + item + quote

    query_string="SELECT ITEM FROM MY_TABLE WHERE MY_ID IN " + sql_list
    ...
Thank you, Janak.

No comments:

Post a Comment