转义Postgres的输入数据 [英] Escape input data for postgres
本文介绍了转义Postgres的输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个python脚本,用于在postgres db中插入数据。
I writing a python script for inserting of data in my postgres db.
postgres中是否有一个转义函数,如何使插入的数据转义?
Is in postgres a escape function how I can escape the inserted data?
推荐答案
只需将查询参数作为第二个参数传递给 execute
,例如:
Just pass query parameters as a second argument to execute
, like:
>>> cur.execute(
... """INSERT INTO some_table (an_int, a_date, a_string)
... VALUES (%s, %s, %s);""",
... (10, datetime.date(2005, 11, 18), "O'Reilly"))
然后,所有参数都将被正确转义。
Then, all of the parameters will be properly escaped.
这是因为后面跟随 c href = http://www.python.org/dev/peps/pep-0249/ rel = nofollow noreferrer> Python数据库API规范v2.0 ,并支持安全的参数化查询。
This is because psycopg2
follows Python Database API Specification v2.0 and supports safe parameterized queries.
另请参阅:
- Parameterized queries with psycopg2 / Python DB-API and PostgreSQL
- psycopg2 equivalent of mysqldb.escape_string?
这篇关于转义Postgres的输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文