I may be missing something obvious, but I can\'t figure out how my code is different from various examples I see in the online documentation for MySQLdb.
I\'m fairly
Interesting. But in the manual there are a couple of examples. Maybe it is something similar.
c=db.cursor()
max_price=5
c.execute("""SELECT spam, eggs, sausage FROM breakfast
WHERE price < %s""", (max_price,))
In this example, max_price=5 Why, then, use %s in the string? Because MySQLdb will convert it to a SQL literal value, which is the string '5'. When it's finished, the query will actually say, "...WHERE price < 5".
And
c.executemany(
"""INSERT INTO breakfast (name, spam, eggs, sausage, price)
VALUES (%s, %s, %s, %s, %s)""",
[
("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
] )
Here we are inserting three rows of five values. Notice that there is a mix of types (strings, ints, floats) though we still only use %s. And also note that we only included format strings for one row. MySQLdb picks those out and duplicates them for each row.
MySQLdb is probably quoting your table names with single quotes instead of backticks. Try this
cursor.execute('SELECT MAX(%%s) FROM `%s`' % table,(countcol))
You cannot use DB-API for metadata; you will need to make replacements yourself outside of the execute()
call.
query = 'SELECT MAX(%%s) FROM `%s`' % (table,)
cursor.execute(query, (countcol,))
Obviously you should not do this if table
comes from an outside source.