0

I'm trying to pass parameters through sqldf in python:

id = '001F5'
q = """select * from df where id= %id; """
test = sqldf(q, globals())

I've tried many things e.g. +id, "+id;"" and %id etc and nothing works, is it possible to do this at all?

Many Thanks

1 Answer 1

3

Try:

id = "'001F5'"
q = "select * from df where id=" + id + ";"
test = sqldf(q, globals())

Or you can try:

id = "'001F5'"
q = "select * from df where id={0};".format(id)
test = sqldf(q, globals())
Sign up to request clarification or add additional context in comments.

2 Comments

let say i have to get the specific column, can i pass those column names as argument also.
@anuragpandey you'd have to build the q format string to incorporate the column names, e.g. q = "select {my_col} from df".format(my_col='Column1'). If it's multiple columns, then add the column names into a list and then set my_col = ','.join(my_list).

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.