1

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:

cols = ["col_a","col_b","col_c"]


query = f"""select '{cols}' from table"""


Current Output : f"""select '["col_a","col_b","col_c"]' from table"""

Expected output: f"""select col_a, col_b, col_c from table"""

1 Answer 1

7

You want to do a join of the list:

query = f"""select {", ".join(cols)} from table"""
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.