Python MySQL parameterized query conflicts with % wildcard in LIKE statement

前端 未结 2 729
無奈伤痛
無奈伤痛 2021-01-16 16:29

My query on execution of this fails:

cursor.execute(\"SELECT name FROM products WHERE rating > %s AND category like \'Automation %\'\", (3));
2条回答
  •  花落未央
    2021-01-16 16:54

    Try to use format. If you have a string, against use %s you can use {}.

    sql = "SELECT name FROM products WHERE rating > {0} AND category like 'Automation %'"
    slq = sql.format(3)
    cursor.execute(sql);
    

    This is much better and compliance to new versions of python. And, plus, you can get annoyed of % in your query.

提交回复
热议问题