prepared statements using psycopg
Asked Answered
R

3

7

I'm a beginner at python. We use this code to execute SQL commands.

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

I wonder is this prepared statement or just a client side quoting?

Ruysdael answered 26/3, 2012 at 3:45 Comment(1)
See here: #6775997Leafstalk
P
21

No, it does not, not for psycopg2 at least. The "Prepare" in the docs refers to a "PREPARE TRANSACTION" which is entirely different than a prepared statement.

You can emulate a prepared statement, by overriding the methods or executing extra statements, however. See: An example of psycopg2 cursor supporting prepared statements

Please see: relevant blog entry for psycopg.

More information:

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

Principally answered 15/4, 2013 at 18:2 Comment(0)
T
0

psycopg does support prepared statements starting with version 3:

https://www.psycopg.org/psycopg3/docs/advanced/prepare.html#prepared-statements

A query is prepared automatically after it is executed more than prepare_threshold times on a connection.

Tenancy answered 7/11, 2021 at 18:11 Comment(0)
C
-8

According to the docs the execute method will "Prepare and execute a database operation (query or command).". So yes, it is a prepared statement.

Curt answered 26/3, 2012 at 3:49 Comment(1)
Actually it currently does not. The "Prepare" in this documentation refers to a "PREPARE TRANSACTION" call required for two-phase commit, not a precompiled SQL statement.Principally

© 2022 - 2024 — McMap. All rights reserved.