Python and MySQLdb: substitution of table resulting in syntax error

Parameter substitution in the DB API is only for values – not tables or fields. You’ll need to use normal string substitution for those:

selectQ ="""SELECT * FROM  %s WHERE %s = %%s;""" % (self.table,self.columnSpecName)
self.db.execute(selectQ,(idKey,))
return self.db.store_result()

Note that the value placeholder has a double % – this is so that it’s left alone by the initial string substitution.

Leave a Comment