Home  >  Q&A  >  body text

How to operate mysql in python to prevent your own program from being maliciously injected later (to put it bluntly, I am asking about some key points of anti-injection in python)

Is it necessary to refer to previous anti-injection documents in languages ​​​​such as php or java? It’s really hard to find anti-injection documentation related to python

滿天的星座滿天的星座2684 days ago752

reply all(2)I'll reply

  • 黄舟

    黄舟2017-06-14 10:52:54

    1.Pass

    cursor.execute("select * from table where name=%s", "name")
    

    Anti-injection.

    2.If passed

    sql = "select * from table where name=%s" % MySQLdb.escape_string(name)
    

    This format requires MySQLdb.escape_string(name) to prevent injection.

    It is recommended to use the first one.

    reply
    0
  • 欧阳克

    欧阳克2017-06-14 10:52:54

    sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
    cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
    

    One thing I know is not to use sql.format("x1", "x2"), but to pass the parameters to cursor.execute for processing

    reply
    0
  • Cancelreply