【问题标题】:Escape input data for postgres为 postgres 转义输入数据
【发布时间】:2013-08-19 23:08:25
【问题描述】:

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

在 postgres 中有一个转义函数,我如何可以转义插入的数据?

【问题讨论】:

    标签: python database postgresql psycopg2


    【解决方案1】:

    只需将查询参数作为第二个参数传递给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 并支持安全的参数化查询。

    另见:

    【讨论】:

    • 非常感谢它帮助了我:)
    猜你喜欢
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2016-07-12
    • 2014-06-15
    • 2010-12-16
    相关资源
    最近更新 更多