I need to query to a database and filter on a where link in (....)
. The issue is the (...)
since that "tuple" is dynamic. If I do it like this:
input_links = get_links() # [<link1>,...]
query = f"SELECT * from myTable where link in {tuple(input_links)}
that works fine unless input_links
contains one link then the last part becomes (link1,)
thus breaking the query.
I've tried using
input_links = get_links() # [<link1>,...]
link_tpl = f'({",".join(input_links)})'
query = f"SELECT * from myTable where link in {link_tpl}
but that encloses the entire argument in the tuple in one quote e.g ('link1,link2,link3')
instead of ('link1','link2','link3')
.
I know I can just do an
if len(input_links)==1:
query = f"SELECT * from myTable where link={input_links[0]}"
else:
f"SELECT * from myTable where link in {tuple(input_links)}
but it just bothers me that I cannot have it as one statement.