我编写了一个 python 脚本,用于在我的 postgres 数据库中插入数据。
postgres 中有一个转义函数,我如何转义插入的数据?
最佳答案
只需将查询参数作为第二个参数传递给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并支持安全参数化查询。
另请参阅:
关于python - postgres 的转义输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292019/