Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the MySQLdb module support prepared statements? [duplicate]

Does MySQLdb support server-side prepared statements? I can't figure this out from its manual.

like image 724
planetp Avatar asked Mar 11 '10 11:03

planetp


People also ask

What is MySQLdb module in Python?

MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and is built on top of the MySQL C API. Packages to Install. mysql-connector-python mysql-python.

What is the difference between prepared statements and parameterized queries?

Prepared statements are statement already interpreted, the DBMS change parameters and the query starts immediately. This is a feature of certain DBMS and you can achieve fast response (comparable with stored procedures). Parametrized statement are just a way you compose the query string in your programming languages.

Can we use prepared statement for select query?

To retrieve data from a table using a SELECT statement with parameter markers, you use the PreparedStatement.

What are prepared statements in python?

A prepared statement is a statement that is parsed once by the database engine and then used over and over even with different values.


2 Answers

Check the MySQLdb Package Comments:

"Parameterization" is done in MySQLdb by escaping strings and then blindly interpolating them into the query, instead of using the MYSQL_STMT API. As a result unicode strings have to go through two intermediate representations (encoded string, escaped encoded string) before they're received by the database.

So the answer is: No, it doesn't.

like image 95
Eugene Yarmash Avatar answered Oct 16 '22 04:10

Eugene Yarmash


It has some kind of parameterization, yes.

Even then, I advise you switch to oursql. It brings a lot of advantages over MySQLdb:

  • oursql has real parameterization.
  • oursql allows text or binary data to be streamed into the database and streamed out of the database, instead of requiring everything to be buffered in the client.
  • oursql can both insert rows lazily and fetch rows lazily.
  • oursql has unicode support on by default.
  • oursql supports python 2.4 through 2.7 without any deprecation warnings on 2.6+ (see PEP 218) and without completely failing on 2.7 (see PEP 328).
  • oursql is licensed under the BSD license.
like image 36
nosklo Avatar answered Oct 16 '22 02:10

nosklo