A good way to escape quotes in a database query string?

后端 未结 9 1263
暖寄归人
暖寄归人 2020-12-09 07:49

I\'ve tried all manner of Python modules and they either escape too much or in the wrong way. What\'s the best way you\'ve found to escape quotes (\", \') in Python?

9条回答
  •  余生分开走
    2020-12-09 08:38

    If using psycopg2, its execute() method has built-in escaping:

    cursor.execute("SELECT column FROM table WHERE column=%s AND column2=%s", (value1, value2))
    

    Note, that you are giving two arguments to execute method (string and tuple), instead of using Python's % operator to modify string.

    Answer stolen from here: psycopg2 equivalent of mysqldb.escape_string?

提交回复
热议问题