Python and MySQLdb: substitution of table resulting in syntax error

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

    Here is a full working example

    def rtnwkpr(tick, table, col):
    
        import MySQLdb as mdb
        tickwild = tick + '%'       
        try:
            con = mdb.connect(host, user, password, db);
            cur = con.cursor()
            selectq = "SELECT price FROM %s WHERE %s LIKE %%s;" % (table, col)
            cur.execute(selectq,(tickwild))
            return cur.fetchall()           
    

提交回复
热议问题