使用psycopg编写的语句

问题描述:

我是python的初学者。我们使用这段代码来执行SQL命令。使用psycopg编写的语句

cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abcdef")) 

我不知道这是准备好的声明还是客户端引用?

+0

在这里看到: http://*.com/questions/6775497/psycopg-get- formatted-sql-instead-of-executed – socketpair 2014-10-26 18:00:16

不,它不是,至少不是psycopg2。文档中的“准备”是指与准备好的陈述完全不同的“准备交易”。

但是,您可以通过重写方法或执行额外的语句来模拟预准备语句。请参阅:An example of psycopg2 cursor supporting prepared statements

请参阅:relevant blog entry for psycopg.

的更多信息:

http://www.postgresql.org/docs/9.2/static/sql-prepare.html
http://www.postgresql.org/docs/current/static/sql-prepare-transaction.html

根据the docs执行方法将“准备并执行数据库操作(查询或命令)”。所以是的,这是一个准备好的声明。

+8

实际上它现在没有。本文档中的“准备”是指两阶段提交所需的“PREPARE TRANSACTION”调用,而不是预编译的SQL语句。 – Brian 2013-04-15 18:16:41