Is there a Python MySQL API that returns prepared statements for reuse?

Neither MySQLdb nor oursql allow returning prepared statements to populate parameters for sequential executions. Are there any others?

At least for .executemany() our language is more experienced than MySQLdb, because the SQL statement is ready only once for all the values ​​presented.

(Does Python support preparing and reusing prepared messages using PostgreSQL?)

+4
source share
1 answer

For your PostgreSQL question, the answer is that for prepared prepared statements, there is no one since at least October last year (I am not a Python programmer, but this is quite common in different languages). PostgreSQL, however, offers an SQL language, so any language can use prepared statements if SQL queries can be executed. For code examples on how to do this in Python, see http://initd.org/psycopg/articles/2012/10/01/prepared-statements-psycopg/

However, there are some serious warnings. One of them is that prepared statements are planned at the first launch, and the plan is reused. This is great for queries where the same plan is equally necessary (say, simple inserts), but it causes problems when the parameters can change so much that new plans may be required. Thus, in most cases, it will probably be normal (there are still unpleasant angular cases to be aware of):

  • INSERT INTO foo (bar) values ($1)
  • SELECT * FROM foo WHERE bar= $1; , assuming the bar is the main key

Something like the following may be unsafe:

  • SELECT * FROM foo WHERE bar < $1
  • SELECT * FROM foo WHERE baz = $1 , where baz can have a significant portion of lines with the same value.
0
source

All Articles