In python, changing MySQL query based on function variables

Posted by ensnare on Stack Overflow See other posts from Stack Overflow or by ensnare
Published on 2010-05-10T03:00:02Z Indexed on 2010/05/10 3:08 UTC
Read the original article Hit count: 408

Filed under:
|
|
|

I'd like to be able to add a restriction to the query if user_id != None ... for example:

"AND user_id = 5" 

but I am not sure how to add this into the below function?

Thank you.

def get(id, user_id=None):

    query = """SELECT *
               FROM USERS
               WHERE text LIKE %s AND
                     id = %s
            """
    values = (search_text, id)

    results = DB.get(query, values)

This way I can call:

get(5)
get(5,103524234) (contains user_id restriction)

© Stack Overflow or respective owner

Related posts about python

Related posts about mysql