psycopg2 equivalent of mysqldb.escape_string?

后端 未结 5 588
迷失自我
迷失自我 2020-12-10 23:53

I\'m passing some values into a postgres character field using psycopg2 in Python. Some of the string values contain periods, slashes, quotes etc.

With MySQL I\'d ju

5条回答
  •  情歌与酒
    2020-12-11 00:43

    Escaping is automatic, you just have to call:

    cursor.execute("query with params %s %s", ("param1", "pa'ram2"))
    

    (notice that the python % operator is not used) and the values will be correctly escaped.

    You can escape manually a variable using extensions.adapt(var), but this would be error prone and not keep into account the connection encoding: it is not supposed to be used in regular client code.

提交回复
热议问题