MySQLdb question

Skip Montanaro skip at pobox.com
Thu Jun 20 11:08:26 EDT 2002


    John> Use the %s thingie:

    John> c.execute('SELECT spam, eggs, sausage FROM %s
    John>            WHERE price < %s', (table_name, max_price))

I'd amend that slightly:

    c.execute('SELECT spam, eggs, sausage FROM %s
               WHERE price < %%s'%table_name, (max_price,))

or

    stmt = ('SELECT spam, eggs, sausage FROM %s
            WHERE price < %%s' % table_name)
    c.execute(stmt, (max_price,))

to make the two substitution ops clearer.

For variable substitution I recommend people always use the DB API argument
quoting facility.  In this case it doesn't matter since presumably max_price
won't need escaping.  For string args it definitely would though.

-- 
Skip Montanaro
skip at pobox.com
consulting: http://manatee.mojam.com/~skip/resume.html





More information about the Python-list mailing list