【发布时间】:2013-08-19 23:08:25
【问题描述】:
我编写了一个用于在我的 postgres db 中插入数据的 python 脚本。
在 postgres 中有一个转义函数,我如何可以转义插入的数据?
【问题讨论】:
标签: python database postgresql psycopg2
我编写了一个用于在我的 postgres db 中插入数据的 python 脚本。
在 postgres 中有一个转义函数,我如何可以转义插入的数据?
【问题讨论】:
标签: python database postgresql psycopg2
只需将查询参数作为第二个参数传递给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"))
然后,所有参数将被正确转义。
这是因为psycopg2 遵循Python Database API Specification v2.0 并支持安全的参数化查询。
另见:
【讨论】: