Python and MySQLdb: substitution of table resulting in syntax error

前端 未结 4 1234
日久生厌
日久生厌 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:21

    Did you mean to write:

    selectQ = """SELECT * FROM %s WHERE %s = %s;""" % (self.table,self.columnSpecName,idKey) #maybe the idkey should be self.idkey? don't know the rest of the code
    
    self.db.execute(selectQ)
    

    and this is just a mistake with string formatting?

    Btw why do you write explicit SQL for this kind of work? better use magical sqlalchemy for python sql manipulation..

提交回复
热议问题