Skip to content

Instantly share code, notes, and snippets.

@pawl
Created August 6, 2014 18:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawl/555e5eecce77d4de0ada to your computer and use it in GitHub Desktop.
Save pawl/555e5eecce77d4de0ada to your computer and use it in GitHub Desktop.
Use List In Bind Parameters - SQLAlchemy
my_list = ['peach', 'grape', 'apple']
query_parameters = {}
counter = 1
for list_item in my_list:
query_parameters["list_item" + str(counter)] = list_item
counter += 1
where_clause = 'fruits IN(:' + ",:".join(query_parameters.keys()) + ')' # create clause to be inserted into query
query_text = db.text("""
SELECT
fruits
FROM table
WHERE """ + where_clause + """
""")
result = db.engine.execute(query_text, **query_parameters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment