转义Postgres的输入数据

Azd*_*325 2 python database postgresql psycopg2

我写了一个python脚本,用于在postgres db中插入数据。

在postgres中是转义函数,如何转义插入的数据?

ale*_*cxe 5

只需将查询参数作为第二个参数传递给execute,例如:

>>> cur.execute(
...     """INSERT INTO some_table (an_int, a_date, a_string)
...         VALUES (%s, %s, %s);""",
...     (10, datetime.date(2005, 11, 18), "O'Reilly"))
Run Code Online (Sandbox Code Playgroud)

然后,所有参数将被正确转义。

这是因为psycopg2遵循Python数据库API规范v2.0并支持安全的参数化查询。

另请参阅: