python mysqldb string formatting
        Posted  
        
            by Daniel
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Daniel
        
        
        
        Published on 2010-03-29T13:47:50Z
        Indexed on 
            2010/03/29
            13:53 UTC
        
        
        Read the original article
        Hit count: 626
        
How do I do this correctly:
I want to do a query like this:
query = """SELECT * FROM sometable 
                    order by %s %s 
                    limit %s, %s;"""
conn = app_globals.pool.connection()
cur = conn.cursor()
cur.execute(query, (sortname, sortorder, limit1, limit2) ) 
results = cur.fetchall()
All works fine but the order by %s %s is not putting the strings in correctly. It is putting the two substitutions in with quotes around them.
So it ends up like:
ORDER BY 'somecol' 'DESC'
Which is wrong should be:
ORDER BY somecol DESC
Any help greatly appreciated!
© Stack Overflow or respective owner