0

I am building a select query and adding elements to it programmatically. The problem is that I need the values tuple to have a trailing comma. How can I do this?

def build_search_query(user_list, make, model, price_min, price_max):
    sql = """SELECT * FROM mycolumn WHERE user_id = %s, """   
    for i, d in enumerate(user_list):        
        if not i == 0: #skip first user because its already in sql
            next_user = "OR user_id = %s, "
            sql += next_user
    age_str = "AND age = %s, "
    height_str = "AND height = %s, "
    price_min_str = "AND price > %s, "
    price_max_str = "AND price < %s "
    sql += age_str
    sql += height_str
    sql += price_min_str
    sql += price_max_str
    tuple1 = tuple(user_list)    
    tuple2 = (age, height, price_min, price_max,)
    values = tuple1 + tuple2 #this is where I need the trailing comma
    return sql, values
3
  • 1
    You can't tell a tuple "hey, you need to have a trailing comma when I print you". The tuple object is ignorant of the logic used to turn it into a string. You might be able to fiddle with the string representation at the point in your code where you convert the tuple to the string, but how that might be done depends on how you're doing that. percent style formatting? str.format? f strings? It's unclear, since we don't know how build_search_query is being called or what happens to the return values. Commented Apr 1, 2019 at 18:32
  • In any case, every database library worth using has its own paramaterization methods which are much better at preventing SQL injection attacks than anything us normal people could write. You should almost never insert values into queries on your own. Commented Apr 1, 2019 at 18:39
  • The syntax has nothing to do on how the tuple is actually parsed as string on runtime. You can't achieve what you are pretending to do. The best idea would be to process the tuple with a generator to return the values formatted as you wish. Plus, if you are just trying to write some SQL on python, maybe you should look at some libraries that would help you ease the process and avoid SQL-Injections and other risks, it's better than reinventing the wheel: github.com/kayak/pypika Commented Apr 1, 2019 at 18:43

2 Answers 2

2

Very briefly, you can't. The value is the internal representation of the tuple. As such, it has no parentheses and no commas: those are merely conveniences for human reading.

By definition, a tuple does not have that trailing comma. Since you're returning the value, rather than a print image (string representation for output), I'm not entirely sure where to lead you from here.

Sign up to request clarification or add additional context in comments.

Comments

1

I have had a look at your code and with selecting anything on any condition in SQL after the first condition there is no comma needed only AND like this

SELECT * FROM mycolumn WHERE user_id = %s

age_str = " AND age = {}".format(something)

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.