I am trying to pass a list of columns into a SQL query in Python. This just returns back the list but not the actual columns as shown:
JavaScript
x
10
10
1
cols = ["col_a","col_b","col_c"]
2
3
4
query = f"""select '{cols}' from table"""
5
6
7
Current Output : f"""select '["col_a","col_b","col_c"]' from table"""
8
9
Expected output: f"""select col_a, col_b, col_c from table"""
10
Advertisement
Answer
You want to do a join
of the list:
JavaScript
1
2
1
query = f"""select {", ".join(cols)} from table"""
2