Python and MySQLdb: substitution of table resulting in syntax error

前端 未结 4 1245
日久生厌
日久生厌 2020-11-27 23:33

I need to dynamically change tables and variables from time to time, so I wrote a python method like this:

    selectQ =\"\"\"SELECT * FROM  %s WHERE %s = %s         


        
4条回答
  •  死守一世寂寞
    2020-11-28 00:20

    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.

提交回复
热议问题